SOAP CXF web 服务请求的编组问题
Issue with marshalling of SOAP CXF webservice request
我正在使用
调用 SOAP 网络服务器
我遇到 WebService 请求调用问题,请求字符串 (XML) 在发送时被格式化。
预期请求:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:serviceRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<referenceNum>5361610270000002</referenceNum>
<client>ABCD</client>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<UserId>USER_1234</UserId>
<data><![CDATA[<ns2:getDetailRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<HEADER>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<serviceType>SYNC</serviceType>
<serviceVersion>1.0</serviceVersion>
</HEADER>
</ns2:getDetailRequest>]]>
</data>
</ns2:serviceRequest>
</soap:Body>
</soap:Envelope>
实际发送请求:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:serviceRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<referenceNum>5361610270000002</referenceNum>
<client>ABCD</client>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<UserId>USER_1234</UserId>
<data><![CDATA[<ns2:getDetailRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<HEADER>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<serviceType>SYNC</serviceType>
<serviceVersion>1.0</serviceVersion>
</HEADER>
</ns2:getAccountDetailRequest>]]>
</data>
</ns2:serviceRequest>
</soap:Body>
</soap:Envelope>
我正在使用 Apache CXF (SOAP) 进行网络服务调用。 Java 对象的编组由 CXF 和 JAXB 库在内部处理。
有没有人遇到这样的问题,即 <[CDATA[ ]]> 中的元素在编组期间被格式化或如何解决这个问题?
我找到了使用拦截器的解决方案。
public class CustomSoapCxfInterceptor extends AbstractSoapInterceptor {
public CustomSoapCxfInterceptor() {
super(Phase.PRE_STREAM);
getAfter().add(AttachmentOutInterceptor.class.getName());
}
@Override
public void handleMessage(SoapMessage message) {
try {
message.put("disable.outputstream.optimization", Boolean.TRUE);
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(message.getContent(OutputStream.class));
CDataXMLStreamWriter cDataXMLStreamWriter = new CDataXMLStreamWriter(writer);
message.setContent(XMLStreamWriter.class, cDataXMLStreamWriter);
} catch (Exception exception) {
DefaultLogger.error(exception);
}
}
}
其中 CDataXMLStreamWriter 扩展了 DelegatingXMLStreamWriter
public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {
private int counter = 1;
private String currentElementName;
public CDataXMLStreamWriter(XMLStreamWriter del) {
super(del);
}
@Override
public void writeCharacters(String text) throws XMLStreamException {
boolean useCData = checkIfCDATAneededForCurrentElement();
if (useCData) {
super.writeCData(text);
} else {
super.writeCharacters(text);
}
}
private boolean checkIfCDATAneededForCurrentElement() {
if ("data".equals(currentElementName))
return true;
return false;
}
public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
currentElementName = local;
super.writeStartElement(prefix, local, uri);
}
@Override
public void writeEndElement() throws XMLStreamException {
if ("data".equals(currentElementName)) {
if (this.counter == 1) {
super.writeEndElement();
}
this.counter++;
} else {
super.writeEndElement();
}
}
}
我正在使用
调用 SOAP 网络服务器我遇到 WebService 请求调用问题,请求字符串 (XML) 在发送时被格式化。
预期请求:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:serviceRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<referenceNum>5361610270000002</referenceNum>
<client>ABCD</client>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<UserId>USER_1234</UserId>
<data><![CDATA[<ns2:getDetailRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<HEADER>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<serviceType>SYNC</serviceType>
<serviceVersion>1.0</serviceVersion>
</HEADER>
</ns2:getDetailRequest>]]>
</data>
</ns2:serviceRequest>
</soap:Body>
</soap:Envelope>
实际发送请求:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:serviceRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<referenceNum>5361610270000002</referenceNum>
<client>ABCD</client>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<UserId>USER_1234</UserId>
<data><![CDATA[<ns2:getDetailRequest xmlns:ns2="urn:example.com/xml/schemas/v1_0/">
<HEADER>
<serviceName>GET.PERSONAL.DETAIL</serviceName>
<serviceType>SYNC</serviceType>
<serviceVersion>1.0</serviceVersion>
</HEADER>
</ns2:getAccountDetailRequest>]]>
</data>
</ns2:serviceRequest>
</soap:Body>
</soap:Envelope>
我正在使用 Apache CXF (SOAP) 进行网络服务调用。 Java 对象的编组由 CXF 和 JAXB 库在内部处理。
有没有人遇到这样的问题,即 <[CDATA[ ]]> 中的元素在编组期间被格式化或如何解决这个问题?
我找到了使用拦截器的解决方案。
public class CustomSoapCxfInterceptor extends AbstractSoapInterceptor {
public CustomSoapCxfInterceptor() {
super(Phase.PRE_STREAM);
getAfter().add(AttachmentOutInterceptor.class.getName());
}
@Override
public void handleMessage(SoapMessage message) {
try {
message.put("disable.outputstream.optimization", Boolean.TRUE);
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(message.getContent(OutputStream.class));
CDataXMLStreamWriter cDataXMLStreamWriter = new CDataXMLStreamWriter(writer);
message.setContent(XMLStreamWriter.class, cDataXMLStreamWriter);
} catch (Exception exception) {
DefaultLogger.error(exception);
}
}
}
其中 CDataXMLStreamWriter 扩展了 DelegatingXMLStreamWriter
public class CDataXMLStreamWriter extends DelegatingXMLStreamWriter {
private int counter = 1;
private String currentElementName;
public CDataXMLStreamWriter(XMLStreamWriter del) {
super(del);
}
@Override
public void writeCharacters(String text) throws XMLStreamException {
boolean useCData = checkIfCDATAneededForCurrentElement();
if (useCData) {
super.writeCData(text);
} else {
super.writeCharacters(text);
}
}
private boolean checkIfCDATAneededForCurrentElement() {
if ("data".equals(currentElementName))
return true;
return false;
}
public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
currentElementName = local;
super.writeStartElement(prefix, local, uri);
}
@Override
public void writeEndElement() throws XMLStreamException {
if ("data".equals(currentElementName)) {
if (this.counter == 1) {
super.writeEndElement();
}
this.counter++;
} else {
super.writeEndElement();
}
}
}