修改ExchangeService 抽象定义msg , xml ,json 间的转换

This commit is contained in:
zhouxiunai
2019-01-08 14:56:52 +08:00
parent 6b4fb1ac60
commit f4613f219e
11 changed files with 136 additions and 14 deletions
@@ -10,10 +10,13 @@ import javax.xml.stream.XMLStreamWriter;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
@@ -24,7 +27,8 @@ import com.gzzn.omms.msgexchangeapi.utils.JsonUtil;
@Service("exchangeService")
public class ExchangeServiceImpl implements IExchangeService {
/*
@Override
public String xmlstrToJson(String xml) throws ExchangeServiceException {
MSG msg = xmlstrToObject(xml,MSG.class); //转具体消息
@@ -91,4 +95,92 @@ public class ExchangeServiceImpl implements IExchangeService {
throw new ExchangeServiceException(e.getMessage(),e);
}
}// end function
*/
@Override
public MSG xmlToMsg(String xmlStr) throws ExchangeServiceException {
try {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
xmlMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()));
return xmlMapper.readValue(xmlStr, MSG.class);
} catch (Exception e) {
throw new ExchangeServiceException(e.getMessage(),e);
}
}
@Override
public String msgToJson(MSG msg) throws ExchangeServiceException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
JsonGenerator jsonGenerator = null;
StringWriter out = new StringWriter();
try {
jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(out);
jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField("META", msg.getMETA());
//
String method = "get" + msg.getMETA().getTYPE().name();
jsonGenerator.writeObjectField(
msg.getMETA().getTYPE().name(),
msg
.getClass()
.getDeclaredMethod(method)
.invoke(msg)
);
jsonGenerator.writeEndObject();
jsonGenerator.flush();
return out.toString();
} catch (Exception e) {
throw new ExchangeServiceException(e.getMessage(),e);
}
}
@Override
public String msgToXml(MSG msg) throws ExchangeServiceException {
try {
XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
StringWriter out = new StringWriter();
XMLStreamWriter sw = xmlOutputFactory.createXMLStreamWriter(out);
// then Jackson components
XmlMapper mapper = new XmlMapper(xmlInputFactory);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
sw.writeStartDocument();
sw.writeStartElement("MSG");
sw.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
sw.writeAttribute("xsi", "http://www.w3.org/2001/XMLSchema-instance", "noNamespaceSchemaLocation", "unisysaodbsis.xsd");
// Write whatever content POJOs...
mapper.writeValue(sw, msg.getMETA());
//
String method = "get" + msg.getMETA().getTYPE().name();
mapper.writeValue(sw,
msg
.getClass()
.getDeclaredMethod(method)
.invoke(msg));
sw.writeEndDocument();
return out.toString();
} catch (Exception e) {
throw new ExchangeServiceException(e.getMessage(),e);
}
}
@Override
public String xmlMsgToJsonMsg(String xml) throws ExchangeServiceException {
MSG msg = xmlToMsg(xml);
String json = msgToJson(msg);
return json;
}
}
@@ -9,8 +9,38 @@ import com.gzzn.omms.msgexchangeapi.exception.ExchangeServiceException;
*
*/
public interface IExchangeService {
/*
public String xmlstrToJson(String xml) throws ExchangeServiceException;
public <T> T xmlstrToObject(String xml,Class<T> valueType) throws ExchangeServiceException;
public String objectToXmlstr(Object object) throws ExchangeServiceException;
public String getCoutmsgsClobMsg(MSG msg) throws ExchangeServiceException;
*/
/**
* xml字符串转msg类
* @param xmlStr
* @return
*/
public MSG xmlToMsg(String xmlStr) throws ExchangeServiceException;
/**
* msg类转json字符串
* @param msg
* @return
*/
public String msgToJson(MSG msg) throws ExchangeServiceException;
/**
* msg类转xml字符串
* @param msg
* @return
*/
public String msgToXml(MSG msg) throws ExchangeServiceException;
/**
* xml 消息字符串转json消息字符串
* @param xml
* @return
*/
public String xmlMsgToJsonMsg(String xml) throws ExchangeServiceException;
}