-->

Spring WS web service. Adding an attachment to the

2020-04-10 21:57发布

问题:

I am really struggling getting Spring-WS to return a response with attachments. I have managed to get an MTOM version to work but this has some pre-requisites on the client as i believe that the client has to be MTOM enabled as well (please correct me if this is not correct).

What i am trying to do now is to use the standard SOAP with attachment implementation using SAAJ and Spring-WS.

To do this i implemented an endpoint that just attaches an image from the local filesystem to the response.

@Endpoint
public class TestEndPoint {

private SaajSoapMessageFactory saajMessageFactory;

@PayloadRoot(namespace="http://ws.mypackage.com", localPart="downloadMessageRequestSaaj")
    @ResponsePayload
    public JAXBElement<DownloadResponseSaajType> invoke(@RequestPayload DownloadMessageRequestSaaj req, MessageContext context ) throws Exception  {

        DownloadResponseSaajType response = new DownloadResponseSaajType();
        DownloadResponseSaajType.PayLoad payload = new DownloadResponseSaajType.PayLoad();  

        DataHandler handler = new javax.activation.DataHandler(new FileDataSource("c:\\temp\\maven-feather.png"));

            SaajSoapMessage message = saajMessageFactory.createWebServiceMessage();
            message.addAttachment("picture", handler);

            context.setResponse(message);

            payload.setMessagePayLoad(handler);

            return objectFactory.createDownloadMessageResponseSaaj(response);  

    }

    public void setSaajMessageFactory(SaajSoapMessageFactory saajMessageFactory){
        this.saajMessageFactory = saajMessageFactory;
    }

    public SaajSoapMessageFactory getSaajMessageFactory(){
        return saajMessageFactory;
    }
}

The Saaj properties are depency injected as shown below:


<sws:annotation-driven/>

<bean id="soapMessageFactory" class="javax.xml.soap.MessageFactory" factory-method="newInstance" />
<bean id="saajMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <constructor-arg ref="soapMessageFactory" />
</bean>

<bean id="myService" class="com.mypackage.TestEndPoint">
    <property name="saajMessageFactory" ref="saajMessageFactory" />
</bean>

When i try to call the above service i get the following error:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Server</faultcode>
         <faultstring xml:lang="en">No adapter for endpoint [public javax.xml.bind.JAXBElement&lt;com.mypackage.ws.DownloadResponseSaajType> com.mypackage.TestEndPoint.invoke(com.mypackage.ws.DownloadMessageRequestSaaj,org.springframework.ws.context.MessageContext) throws java.lang.Exception]: Is your endpoint annotated with @Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?</faultstring>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Edit 13th July

I noticed today that i change the method signature to remove the MessageContext parameters as shown below then i dont get that error:

public JAXBElement<DownloadResponseSaajType> invoke(@RequestPayload DownloadMessageRequestSaaj req)

The problem however is that i need to access the MessageContext to be able to add the attachment. Could be that maybe my configuration is wrong somewhere?

回答1:

I believe in either case your client will need to be aware of the attachment so I would recommend sticking with mtom (as it is becoming the standard)

check which version of spring-ws you are using and which maven group-id you are using. i was getting the same error because this feature was recently added (I think?).

try this entry and remove any other spring-ws imports your making

    <dependency>
        <groupId>org.springframework.ws</groupId>
        <artifactId>spring-ws-core</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>