我正在发送SOAP请求,服务器抱怨SOAPAction标头为空。我想我做对了,但显然我不是。 Wireshark显示它没有设置。
@Test
public void testLogin() throws Exception {
StringBuffer loginXml = new StringBuffer();
loginXml.append("<soapenv:Envelope xmlns:soapenv=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns=\"http://example.com/xyz/2010/08\">");
loginXml.append(" <soapenv:Header>");
loginXml.append(" <ns:loginOperationDetails>");
loginXml.append(" </ns:loginOperationDetails>");
loginXml.append(" </soapenv:Header>");
loginXml.append(" <soapenv:Body>");
loginXml.append(" <ns:LogIn>");
loginXml.append(" <ns:logInInfo>");
loginXml.append(" <ns:CustomerAccountId>customer1</ns:CustomerAccountId>");
loginXml.append(" <ns:Username>JDoe</ns:Username>");
loginXml.append(" <ns:Password>abc123</ns:Password>");
loginXml.append(" </ns:logInInfo>");
loginXml.append(" </ns:LogIn>");
loginXml.append(" </soapenv:Body>");
loginXml.append("</soapenv:Envelope>");
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SaajSoapMessageFactory newSoapMessageFactory = new SaajSoapMessageFactory(msgFactory);
webServiceTemplate.setMessageFactory(newSoapMessageFactory);
String uri = "http://xyz.example.com/xyz_1.0/membership.svc/ws";
webServiceTemplate.setDefaultUri(uri);
StreamSource source = new StreamSource(new StringReader(loginXml.toString()));
StreamResult result = new StreamResult(System.out);
boolean resultReturned = false;
try {
resultReturned = webServiceTemplate.sendSourceAndReceiveToResult(source,
new SoapActionCallback("http://example.com/xyz/2010/08/MembershipService/LogIn"),
result);
}
catch (SoapFaultClientException sfe) {
logger.error("SoapFaultClientException resultReturned: " + resultReturned, sfe);
fail();
}
}
我从服务器回来的错误说:
500 Internal Server Error
The SOAP action specified on the message, '', does not match the HTTP SOAP Action, 'http://example.com/xyz/2010/08/MembershipService/LogIn'.
答案 0 :(得分:36)
当您使用WebServiceTemplate
作为类与Web服务进行通信时,我不明白为什么,但它没有正确填充HTTP标头。
一些WSDL有一个部分说:
<soap:operation
soapAction="SOMELINK"
style="document" />
而WebServiceTemplate
忽略了这一部分。上述错误表示标头中的soapAction
参数为空。它应该不是。查看Wireshark。我做了 - 使用一些Chrome Soap客户端和Spring。第二个标题无效。
要解决此问题,您需要按照此处的第6.2.4节进行操作:http://docs.spring.io/spring-ws/sites/2.0/reference/html/client.html
它所说的基本上是使用WebServiceMessageCallback
接口自行添加标题部分。您可以在参考文献中阅读更多内容。
基本上它最终会像这样:
webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
((SoapMessage)message).setSoapAction("http://tempuri.org/Action");
}
});
您可以在哪里正确设置标头值。也为我工作。整天阅读。
答案 1 :(得分:4)
我解决了这个问题,但从未发布过答案。以下是我最终得到的结果:
public WebServiceTemplate getWebServiceTemplate() throws SOAPException {
if (webServiceTemplate == null) {
final MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
final SaajSoapMessageFactory newSoapMessageFactory = new SaajSoapMessageFactory(msgFactory);
webServiceTemplate = new WebServiceTemplate(newSoapMessageFactory);
}
return webServiceTemplate;
}
public Object sendReceive(Object requestObject, ArrayList<String> classesToMarshall, final String action)
throws ClassNotFoundException, SoapFaultException, SoapFaultClientException, WebServiceTransportException,
IllegalStateException, SOAPException {
final WebServiceTemplate wst = getWebServiceTemplate();
final SoapMarshallUtil smu = getSoapMarshallUtil();
smu.configureMarshaller(wst, classesToMarshall);
// soap 1.2
SoapActionCallback requestCallback = new SoapActionCallback(action) {
public void doWithMessage(WebServiceMessage message) {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SoapHeader soapHeader = soapMessage.getSoapHeader();
QName wsaToQName = new QName("http://www.w3.org/2005/08/addressing", "To", "wsa");
SoapHeaderElement wsaTo = soapHeader.addHeaderElement(wsaToQName);
wsaTo.setText(uri);
QName wsaActionQName = new QName("http://www.w3.org/2005/08/addressing", "Action", "wsa");
SoapHeaderElement wsaAction = soapHeader.addHeaderElement(wsaActionQName);
wsaAction.setText(action);
}
};
Object responseObject = wst.marshalSendAndReceive(this.uri, requestObject, requestCallback);
return responseObject;
}
答案 2 :(得分:0)
另一种解决方法是添加 interceptor 并在handleRequest()
方法中设置soapAction,以使入站接收MessageContext
,{派生;
之后,您可以轻松设置调用SoapMessage
方法的soapAction。
这是拦截器类的代码:
setSoapAction()
,当然还要将拦截器添加到public class SecurityInterceptor implements ClientInterceptor {
@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
SoapMessage soapMessage = (SoapMessage) messageContext.getRequest();
soapMessage.setSoapAction("mySoapAction");
return true;
}
//TODO:: other methods and constructor..
}
:
WebTemplate