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

标签 java spring web-services maven spring-ws

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

服务在此公开: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 字符串委托(delegate)给 Spring 的 WebServiceTemplate 并期望在 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>";

最佳答案

我认为你的问题与soapActionCallBack有关。您仅调用该操作,而不是使用 uri 调用该操作。以下是来自 here 的示例:

如果您备注命名空间为 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 (不过我不太确定大小写)

关于java - Spring: WebServiceTransportException: Not Found [404] 对于非常简单的 WS 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18767943/

相关文章:

json - 如何在 Angular 2 中调用返回单个 JSON 对象的 Web 服务?

java - Solr 是否有 xml 替代 @Field 注释?

java - Spring启动OAuth2实现: NoSuchBeanDefinitionException: No qualifying bean of type AuthenticationManager

java - Spring中MappingSqlQuery的通用实现

java - 构建 WebService 应用程序的推荐方法是什么?

java - JAX-WS 始终内联发送 MTOM 附件

java - 为什么我可以匿名子类化枚举而不是最终类?

java - 无法配置 spring boot 安全 - 总是 403

java - 为什么我得到 "the receiver expression should be replaced with the type qualifier"?

java - CXF、WS-Security 和 Spring : UserToken