1

我正在尝试为公开服务编写客户端。我只需要传递原始 XML(作为 Java 字符串)并查看原始响应。不需要绑定。

服务公开在这里:http ://www.webservicex.net/periodictable

我验证了以下请求 XML工作正常(通过 Soap UI):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetAtomicWeight>
         <!--Optional:-->
         <web:ElementName>Aluminium</web:ElementName>
      </web:GetAtomicWeight>
   </soapenv:Body>
</soapenv:Envelope>

我在这里使用 Spring 的 WS 模板,Maven 依赖项:

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

现在,这是我简单的“主要”应用程序。它只是将 XML 字符串委托给 SpringWebServiceTemplate并期望在System.out.

package sk.xorty.ws;

import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;

public class WsTest {

    private static final String REQUEST_PERIODIC =
            "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:web=\"http://www.webserviceX.NET\">\n" +
            "   <soap:Header/>\n" +
            "   <soap:Body>\n" +
            "      <web:GetAtomicWeight>\n" +
            "         <web:ElementName>Aluminium</web:ElementName>\n" +
            "      </web:GetAtomicWeight>\n" +
            "   </soap:Body>\n" +
            "</soap:Envelope>\n";

    private static final String URL_PERIODIC = "http://www.webservicex.net/periodictable";

    private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    // send to an explicit URI
    public void customSendAndReceive() throws SOAPException {
        MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
        SaajSoapMessageFactory newSoapMessageFactory = new SaajSoapMessageFactory(messageFactory);
        webServiceTemplate.setMessageFactory(newSoapMessageFactory);

        StreamSource source = new StreamSource(new StringReader(REQUEST_PERIODIC));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult(URL_PERIODIC, source,
                new SoapActionCallback("GetAtomicWeight"), result);
    }

    public static void main(String[] args) throws SOAPException {
        new WsTest().customSendAndReceive();
    }
}

示例是可运行的(它只需要类路径中的上述依赖项)。它抛出以下错误:

INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
Exception in thread "main" org.springframework.ws.client.WebServiceTransportException: Not Found [404]
    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 sk.xorty.ws.WsTest.customSendAndReceive(WsTest.java:38)
    at sk.xorty.ws.WsTest.main(WsTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

编辑: 请求的正确值是这个:

private static final String REQUEST_PERIODIC = "<web:GetAtomicWeight xmlns:web=\"http://www.webserviceX.NET\">\n" +
        "         <!--Optional:-->\n" +
        "         <web:ElementName>Aluminium</web:ElementName>\n" +
        "      </web:GetAtomicWeight>";
4

4 回答 4

1

我认为您的问题与soapActionCallBack 有关。您只调用操作而不是使用 uri 的操作。以下是此处的示例:

如果您备注命名空间是http://tempuri.org并且为 soapActionCallback 提供的参数是http://tempuri.org/SOAPACTION *

 WebServiceTemplate template = new WebServiceTemplate(messageFactory);
 Result result = new DOMResult();
 template.sendSourceAndReceiveToResult(
     new StringSource("<content xmlns=\"http://tempuri.org\"/>"),
     new SoapActionCallback("http://tempuri.org/SOAPAction"),
     result);

在你的情况下,肥皂动作应该是http://www.webserviceX.net/GetAtomicWeight(虽然我不太确定大写)

于 2013-09-12T18:15:59.327 回答
1

如果您使用注解,请确保以下注解的名称空间属性与相应 WSDL 元素的名称空间属性匹配:

@XmlRootElement(namespace = "http://webService.mydomain.com", name = "someSOAPRequest")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {      
        "xmlTag#1",
        "xmlTag#2",
        "xmlTag#3",
        "xmlTag#4",
},  namespace = "http://webService.mydomain.com", name = "someSOAPRequest")

如果您不使用注释,请确保代码和 WSDL 元素之间的名称空间属性匹配。

于 2015-09-01T05:03:34.387 回答
0

我有同样的错误,我使用的是 Soap1.1,我注意到 WS 是通过 @SoapAction 注释被 Spring 暴露的,而我使用 Spring WS 和 JAXB 生成的客户端默认没有设置任何soapAction,所以我不得不使用 SoapActionCallback 使用 WSDL 中为该操作指定的 SoapAction 对其进行设置。

 SoapActionCallback soapActionCallback = new SoapActionCallback("restituisciStatiMessaggioListRequestSA");
    RestituisciStatiMessaggioListResponse response = (RestituisciStatiMessaggioListResponse) getWebServiceTemplate().marshalSendAndReceive(request,soapActionCallback);
于 2014-06-25T12:29:39.767 回答
0

我通过从消息中删除 SOAP 请求标头解决了我的问题。所以让 spring 担心 SOAP 请求,而您的代码只是发送您的请求数据。

所以试试:

   private static final String REQUEST_PERIODIC =
               "<web:GetAtomicWeight xmlns:web=\"http://www.webserviceX.NET\">\n" +
               "    <web:ElementName>Aluminium</web:ElementName>\n" +
               "</web:GetAtomicWeight>\n";
于 2014-11-21T21:46:52.247 回答