-->

how do i get a soap attachment from web service us

2019-06-13 01:04发布

问题:

I am using Spring-ws. I am trying WebServiceTemplate.sendSourceAndReceive(src, mySourceExtractor); In the extractData callback method of the SourceExtractor, I get a DOMSource. Now what?

I know there is an attachment because when I call the service with soapUI, I can get the attachment (pdf file).

Is there a better way? Thanks.

回答1:

use a ClientInterceptor:

public class AttachmentInterceptor implements ClientInterceptor {
Iterator<Attachment> attachments;

@Override
public boolean handleResponse(MessageContext msgCtx) throws WebServiceClientException {
    WebServiceMessage msg = msgCtx.getResponse();
    if (msg instanceof SaajSoapMessage) {
        SaajSoapMessage sm = (SaajSoapMessage) msg;
        attachments = sm.getAttachments();
        return false;
    } else {
        return true;
    }
}

public Iterator<Attachment> getAttachments() {
    return attachments; 
}...

then attach interceptor to WebServiceTemplate and execute call:

    AttachmentInterceptor ai = new AttachmentInterceptor();
    wst.setInterceptors(new ClientInterceptor [] { ai });
    wst.sendSourceAndReceiveToResult(src, result);

    /**
     * get attachment
     */
    Iterator<Attachment> it = ai.getAttachments();

then process the attachments.