无法让Spring SOAP Client工作:内容类型'text / xml; charset = utf-8'不是预期的类型'application / soap + xml;字符集= UTF-8'

时间:2013-01-27 15:09:55

标签: spring soap soap-client spring-ws

嗨,我正在尝试使用Spring-ws使一个简单的肥皂客户端工作。我在这个错误上做的谷歌搜索说我正在使用Soap 1.1并且需要指定Soap 1.2。我试过这样做。我在下面正确地做了吗?如果这不是问题,有人看到问题是什么?

这是堆栈跟踪的一大块:

org.springframework.ws.client.WebServiceTransportException: Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. [415]
    at org.springframework.ws.client.core.WebServiceTemplate.handleError(WebServiceTemplate.java:663)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:587)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:492)
    at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:436)
    at com.jda.fileserver.FujiAuthenticationTest.testLogin(FujiAuthenticationTest.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

这是我的代码,感谢您试图提供帮助:

public class AuthTest {
@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://abc.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>jsmith</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();
        SaajSoapMessageFactory defaultMessageFactory = (SaajSoapMessageFactory) webServiceTemplate.getMessageFactory();
        defaultMessageFactory.setSoapVersion(SoapVersion.SOAP_12);
        webServiceTemplate.setMessageFactory(defaultMessageFactory); // probably not needed

        StreamSource source = new StreamSource(new StringReader(loginXml.toString()));
        StreamResult result = new StreamResult(System.out);
        String uri = "http://xyz.abcstage.com/xyz_1.0/membership.svc/ws";
        SoapActionCallback requestCallback = new SoapActionCallback("http://abc.com/xyz/2010/08/MembershipService/LogIn");
        try {
            webServiceTemplate.sendSourceAndReceiveToResult(uri, source, requestCallback, result);
        } 
        catch (SoapFaultException sfe) {
            throw new Exception("SoapFaultException", sfe);
        }
        catch (WebServiceTransportException wste) {
            throw new Exception("WebServiceTransportException", wste);
        }
    }
}

4 个答案:

答案 0 :(得分:1)

好的,我解决了上面的问题,这让我遇到了另一个问题。首先,我是如何解决上述问题的。现在我没有在SaajSoapMessageFactory上设置SOAP版本,我将它设置在包装的MessageFactory上。现在我的请求中出现的Content-Type是application / soap + xml。

WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SaajSoapMessageFactory newSoapMessageFactory = new SaajSoapMessageFactory(msgFactory);
webServiceTemplate.setMessageFactory(newSoapMessageFactory);

下一个问题,现在我得到了这个:

org.springframework.ws.soap.client.SoapFaultClientException: Unexpected fault in the service.
    at org.springframework.ws.soap.client.core.SoapFaultMessageResolver.resolveFault(SoapFaultMessageResolver.java:37)
    at org.springframework.ws.client.core.WebServiceTemplate.handleFault(WebServiceTemplate.java:774)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:600)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:492)
    at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:436)

以及响应中返回的信息:

500 Internal Server Error
The SOAP action specified on the message, '', does not match the HTTP SOAP Action, 'http://abc.com/xyz/2010/08/MembershipService/LogIn'.

我会尝试解决这个问题,但是想要更新任何阅读此内容的人,以便他们可以停止查看上一个错误。我需要弄清楚如何正确设置soap动作。

答案 1 :(得分:1)

请使用以下代码在Spring Web服务模板marshallSendAndReceive方法中将标头内容类型更改为text / xml; charset = utf-8。

  WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
webServiceTemplate.marshalSendAndReceive(url, request, new WebServiceMessageCallback() {

                public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
                    SaajSoapMessage soapMessage = (SaajSoapMessage) message;
                    MimeHeaders headers = soapMessage.getSaajMessage().getMimeHeaders();
                    headers.addHeader(TransportConstants.HEADER_CONTENT_TYPE, "text/xml;charset=utf-8");

答案 2 :(得分:0)

要添加带有动作和标签的soapheader,以下代码对我来说很好。

public void doWithMessage(WebServiceMessage消息)引发IOException {       SaajSoapMessage soapMessage =(SaajSoapMessage)消息;

        SoapEnvelope soapEnvelope = soapMessage.getEnvelope();
        SoapHeader soapHeader = soapEnvelope.getHeader();

        //Initialize QName for Action and To 
        QName action = new QName("{uri}","Action","{actionname}");
        QName to = new QName("{uri}","To","{actionname}");


        soapHeader.addNamespaceDeclaration("{actionname}", "{uri}");


        SoapHeaderElement soapHeaderElementAction = soapHeader.addHeaderElement(action);
        SoapHeaderElement soapHeaderElementTo =  soapHeader.addHeaderElement(to);



        soapHeaderElementAction.setText("{text inside the tags}");


        soapHeaderElementTo.setText("{text inside the tags}");


        soapMessage.setSoapAction("{add soap action uri}");


        soapMessage.writeTo(out);

}

答案 3 :(得分:0)

使用 setHeader 因为您可能想要替换现有标题的内容或在它不存在时添加它。尽管 Httpheaders 应该不区分大小写,但使用 addHeader 就可以做到这一点。在调用 addHeader() 时调试源代码,即使它忽略大小写,您也会看到它在另一个标头之后插入新标头。 所以,至少在 Java 中,会以

内容类型:

内容类型:

            public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
                SaajSoapMessage soapMessage = (SaajSoapMessage) message;
                MimeHeaders headers = soapMessage.getSaajMessage().getMimeHeaders();
                headers.setHeader(TransportConstants.HEADER_CONTENT_TYPE, "text/xml;charset=utf-8");