Creating a Spring Web Service using Plain Old XML (POX)

一个人想着一个人 提交于 2021-02-08 08:46:18

问题


So, I'm writing a Spring Web Service for an already-existing application to talk to. I have the service working to where it will receive a request and respond to it but the thing is, the application expects the response to be in Plain Old XML (POX). My response currently has SOAP headers attached to it like so:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
        <Person>
            <FirstName>John</FirstName>
            <LastName>Smith</LastName>
        </Person>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

What I need it to return is this:

<Person>
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
</Person>

From what I've found, Spring is very much capable of doing this and I think it has something to do with DomPoxMessageFactory but all the examples I've found have been with old versions of Spring WS like v1.5. Even the POX sample in the Spring distribution was written 3 years ago. Thanks for any help!

EDIT: I am also using spring-ws v2.1.2. My code so far very much resembles what you will find here: http://static.springsource.org/spring-ws/sites/2.0/reference/html/tutorial.html


回答1:


You could implement an EndpointInterceptorAdapter and change the response overriding the handleResponse.:

@Override
public boolean handleResponse(MessageContext messageContext_, Object endpoint_) 
throws IOException {

WebServiceMessage _webServiceMessage = messageContext_.getResponse();
SoapMessage _soapMessage = (SoapMessage) _webServiceMessage;
// modify the content

But i am not sure to what extend you could modify the response.




回答2:


For me, all I needed was this in the java-based configuration:

@Bean(name = "messageFactory")
public WebServiceMessageFactory messageFactory() {
    return new DomPoxMessageFactory();
}

This means MessageDispatcherServlet will switch to POX instead of default SOAP.

If you're using XML-based config, this should do it:

<bean id="messageFactory" class="org.springframework.ws.pox.dom.DomPoxMessageFactory" />

From comment by @loshad-vtapkah:

This modification only works if namespace is specified in request.xml. For case specified on the link you've provided, namespace is omitted. So you will need either set empty namespace for CountryEndpount (check PayloadRoot annotation) or extend DomPoxMessage.createWebServiceMessage() method to update document nodes namespace.



来源:https://stackoverflow.com/questions/15486140/creating-a-spring-web-service-using-plain-old-xml-pox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!