Java Spring: WebServiceTransportException: Not Found [404] 对于非常简单的 WS 客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18767943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:52:58  来源:igfitidea点击:

Spring: WebServiceTransportException: Not Found [404] for very simple WS client

javaspringweb-servicesmavenspring-ws

提问by Xorty

I am trying to write a client for publicly exposed service. I only need to pass raw XML(as Java string) and view raw response. No binding is required.

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

Service is publicly exposed here: http://www.webservicex.net/periodictable

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

I verified that following request XML works correctly(via Soap UI):

我验证了以下请求 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>

I am using Spring's template for WS, Maven dependency here:

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

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

Now, here is my simple "main" application. It just delegates XML string to Spring's WebServiceTemplateand expect to see result in System.out.

现在,这是我的简单“主要”应用程序。它只是将 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();
    }
}

Example is runnable (it only needs dependency above in classpath). It throws following error:

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

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)

EDIT:Correct value for request is this one:

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

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>";

采纳答案by VirtualTroll

I think your problem has to do with the soapActionCallBack. You are calling the operation only not the operation with the uri. Below is the example from here:

我认为您的问题与soapActionCallBack 有关。您只调用操作而不是使用 uri 调用操作。下面是这里的例子:

If you remark the namespace is http://tempuri.organd the provided argument for soapActionCallback is http://tempuri.org/SOAPACTION*

如果您备注命名空间是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);

In your case, the soap action should be http://www.webserviceX.net/GetAtomicWeight( I am not so sure about the capitalization though)

在您的情况下,soap 操作应该是http://www.webserviceX.net/GetAtomicWeight(不过我不太确定大小写)

回答by Orazio

I had the same error and I was using Soap1.1, I've noticed that the WS was exposed by Spring throgh the @SoapAction annotation and my client generated with Spring WS and JAXB did not set any soapAction by default, so I had to use a SoapActionCallback to set it with the SoapAction specified in WSDL for that operation.

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

 SoapActionCallback soapActionCallback = new SoapActionCallback("restituisciStatiMessaggioListRequestSA");
    RestituisciStatiMessaggioListResponse response = (RestituisciStatiMessaggioListResponse) getWebServiceTemplate().marshalSendAndReceive(request,soapActionCallback);

回答by Serge Merzliakov

I fixed my issue by removing SOAP request headers from my message. So let spring worry about the SOAP request, and your code just sends your request data.

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

So try:

所以尝试:

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

回答by RHMartok

If you are using annotations, make sure the namespace attribute of the following annotations match the namespace attribute of the corresponding WSDL Elements:

如果您使用注解,请确保以下注解的命名空间属性与相应 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")

If you are not using annotations make sure the namespace attributes match between the code and the WSDL elements.

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