我真的很难让Spring-WS返回带附件的响应。我已经设法让MTOM版本工作,但这在客户端有一些先决条件,因为我相信客户端也必须启用MTOM(如果这不正确,请纠正我)。
我现在要做的是使用标准的SOAP和附件实现,使用SAAJ和Spring-WS。
为此,我实现了一个端点,只需将图像从本地文件系统附加到响应即可。
@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;
}
}
Saaj属性是依赖注入,如下所示:
<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>
当我尝试调用上述服务时,我收到以下错误:
<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<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>
我今天注意到我更改了方法签名以删除MessageContext参数,如下所示,然后我没有收到该错误:
public JAXBElement<DownloadResponseSaajType> invoke(@RequestPayload DownloadMessageRequestSaaj req)
然而问题是我需要访问MessageContext才能添加附件。也许这可能是我的配置在某处错了?
答案 0 :(得分:1)
我相信在任何一种情况下你的客户都需要知道附件,所以我建议坚持使用mtom(因为它正在成为标准)
检查您正在使用的spring-ws版本以及您正在使用的maven group-id。我得到了同样的错误,因为最近添加了这个功能(我认为?)。
尝试此条目并删除所有其他弹簧导入您的制作
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>