转换cmoutmsg消息的xml 去除多余的空标签

This commit is contained in:
zhouxiunai
2019-01-07 17:28:33 +08:00
parent 216b71ad1a
commit bd5fc8191d
5 changed files with 50 additions and 2 deletions
+6
View File
@@ -57,6 +57,12 @@
</dependency>
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
@@ -8,6 +8,8 @@
package com.gzzn.omms.msgexchangeapi.entity.msg;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.io.Serializable;
import java.util.ArrayList;
@@ -184,6 +186,7 @@ public class MSG
implements Serializable
{
private final static long serialVersionUID = 1L;
@XmlElement(name = "META", required = true)
@JsonProperty("META")
@@ -57,7 +57,7 @@ public class CmoutmsgServiceImpl implements ICmoutmsgService {
//发送消息到数据库
Cmoutmsg cmoutmsg = new Cmoutmsg();
cmoutmsg.setCoutmsgsDateInserted(new Date());
cmoutmsg.setCoutmsgsClobMsg(exchangeService.objectToXmlstr(rqfdMsg));
cmoutmsg.setCoutmsgsClobMsg(exchangeService.getCoutmsgsClobMsg(rqfdMsg));
cmoutmsg.setRoutingid(SUBSYSTEMS.OSH5RQFD.name());
Cmoutmsg rtCmoutmsg = cmoutmsgDao.save(cmoutmsg);
@@ -1,9 +1,15 @@
package com.gzzn.omms.msgexchangeapi.service.exchange;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -44,7 +50,6 @@ public class ExchangeServiceImpl implements IExchangeService {
@Override
public String objectToXmlstr(Object object) throws ExchangeServiceException {
try {
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
@@ -54,4 +59,36 @@ public class ExchangeServiceImpl implements IExchangeService {
throw new ExchangeServiceException(e.getMessage(),e);
}
}
@Override
public String getCoutmsgsClobMsg(MSG msg) {
try {
// First create Stax components we need
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());
mapper.writeValue(sw, msg.getRQFD());
sw.writeEndDocument();
return out.toString();
} catch (Exception e) {
throw new ExchangeServiceException(e.getMessage(),e);
}
}// end function
}
@@ -1,5 +1,6 @@
package com.gzzn.omms.msgexchangeapi.service.exchange;
import com.gzzn.omms.msgexchangeapi.entity.msg.MSG;
import com.gzzn.omms.msgexchangeapi.exception.ExchangeServiceException;
/**
@@ -11,4 +12,5 @@ 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;
}