SpringWS 如何在没有base64数据的情况下记录MTOM消息



我使用 SpringWS (SaajSoapMessage(SOAPMessage.writeTo(OutputStream( 来记录原始 xml 消息。一个问题是它还记录了base64。我该如何解决它。

另一个问题似乎不像是运输mtom attchment的方式。 我很困惑

public WSResponse extractData(WebServiceMessage message) throws IOException,
TransformerException {
//      SoapHeader soapHeader = ((SoapMessage)message).getSoapHeader();
//      Iterator<SoapHeaderElement> it = soapHeader.examineHeaderElements(qName);
//      SoapHeaderElement header = it.hasNext() ?it.next() : null;
//      WSResponse wsresponse = new WSResponse();
//      JAXBElement<CommonResponseBody> response = (JAXBElement<CommonResponseBody>)
//              customizeUnmarshaller.unmarshal(message.getPayloadSource());
//      JAXBElement<WSResponseHeader> responseHeader = 
//               (JAXBElement<WSResponseHeader>) customizeUnmarshaller.unmarshal(header.getSource());
//      wsresponse.setResponseHeader(responseHeader.getValue());
//      wsresponse.setResponseBody(response.getValue());
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
message.writeTo(baos);
String temp = baos.toString();
String toReturn = "";

响应正文

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="", propOrder={"ResponseBody", "Status", "file"})
public class CommonResponseBody {
private String ResponseBody;
private String Status;
private DataHandler file;
public String getResponseBody() {
return ResponseBody;
}
public void setResponseBody(String requestBody) {
ResponseBody = requestBody;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public DataHandler getFile() {
return file;
}
public void setFile(DataHandler file) {
this.file = file;
}       
}

会议

<!-- ws 客户端 -->
<bean id="saajSoapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_11"/>
</property>
</bean>
<bean id="completeJaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan" value="com.bytedusk.dev.ws.client.complete.model"></property>
<property name="mtomEnabled" value="true"></property>
</bean>
<bean id="completeWebServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="saajSoapMessageFactory"></constructor-arg>
<property name="marshaller" ref="completeJaxb2Marshaller"></property>
<property name="unmarshaller" ref="completeJaxb2Marshaller"></property>
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender"></bean>
</property>
<property name="defaultUri" value="http://localhost:8080/spring-demo-web/ws/getRegistInfo"> </property>     
</bean> 

被Wireshark捕获的消息

<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<ns3:ResponseHeader
xmlns:ns3="http://ws.dev.bytedusk.com/">
<SvrTime>
20200520 10:29:17:716
</SvrTime>
<LogId>
1589941757716
</LogId>
</ns3:ResponseHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns3:getRegistInfoResponse
xmlns:ns3="http://ws.dev.bytedusk.com/">
<ResponseBody>
server response @20200520 10:29:17:716
</ResponseBody>
<Status>
server status
</Status>
<file>
[truncated]/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCAEXAlgDAS
</file>
</ns3:getRegistInfoResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我找到了薮猫解决方案,但它们对我不起作用

https://stackoverflow.com/a/28719885/9628318

// Get the Envelope Source 
Source src = message.getSOAPPart().getContent() ;
// Transform the Source into a StreamResult to get the XML
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(src, result);
String xmlString = result.getWriter().toString();

尝试这个时,我更改为公理消息,它也不起作用

在不内联优化二进制数据的情况下记录 MTOM 消息

http://ws.apache.org/axiom/quickstart-samples.html

private void logMessage1(SOAPEnvelope env) throws XMLStreamException {
StringWriter sw = new StringWriter();
XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(sw);
XMLStreamWriter encoder = new XOPEncodingStreamWriter(writer,
ContentIDGenerator.DEFAULT, OptimizationPolicy.DEFAULT);
env.serialize(encoder);
log.info("Message: " + sw.toString());
}

最后!!,我发现我使用编组器的方式错误。

public void doWithMessage(WebServiceMessage message) throws IOException,
TransformerException {      
SoapHeader soapHeader = ((SoapMessage)message).getSoapHeader();
SoapBody soapBody = ((SoapMessage)message).getSoapBody();
com.bytedusk.dev.ws.client.complete.model.ObjectFactory objectFactory = 
new com.bytedusk.dev.ws.client.complete.model.ObjectFactory();
JAXBElement<WSRequestHeader> headers =
objectFactory.createWSRequestHeader((WSRequestHeader) header);
new com.bytedusk.dev.ws.client.complete.model.ObjectFactory();
JAXBElement<CommonRequestBody> bodys =
objectFactory.createCommonRequestBody((CommonRequestBody) body);
try {
marshaller.marshal(headers, soapHeader.getResult());
marshaller.marshal(bodys, soapBody.getPayloadResult());
}

当前方式

public void doWithMessage(WebServiceMessage message) throws IOException,
TransformerException {      
SoapHeader soapHeader = ((SoapMessage)message).getSoapHeader();
SoapBody soapBody = ((SoapMessage)message).getSoapBody();
com.bytedusk.dev.ws.client.complete.model.ObjectFactory objectFactory = 
new com.bytedusk.dev.ws.client.complete.model.ObjectFactory();
JAXBElement<WSRequestHeader> headers =
objectFactory.createWSRequestHeader((WSRequestHeader) header);
new com.bytedusk.dev.ws.client.complete.model.ObjectFactory();
JAXBElement<CommonRequestBody> bodys =
objectFactory.createCommonRequestBody((CommonRequestBody) body);
try {
marshaller.marshal(headers, soapHeader.getResult());
MarshallingUtils.marshal(marshaller, bodys, message);
}

请注意,它们调用不同的编组方法

@Override
public void marshal(Object graph, Result result) throws XmlMappingException {
marshal(graph, result, null);
}
@Override
public void marshal(Object graph, Result result, MimeContainer mimeContainer) throws XmlMappingException {
try {
Marshaller marshaller = createMarshaller();
if (this.mtomEnabled && mimeContainer != null) {
marshaller.setAttachmentMarshaller(new Jaxb2AttachmentMarshaller(mimeContainer));
}
if (StaxUtils.isStaxResult(result)) {
marshalStaxResult(marshaller, graph, result);
}
else {
marshaller.marshal(graph, result);
}
}
catch (JAXBException ex) {
throw convertJaxbException(ex);
}
}

相关内容

  • 没有找到相关文章

最新更新