我正在尝试为公开服务编写客户端。我只需要传递原始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字符串委托给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();
}
}
示例是runnable(它只需要在classpath中具有依赖性)。它抛出以下错误:
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>";
答案 0 :(得分:1)
我认为你的问题与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(虽然我不太确定资本化)
答案 1 :(得分:1)
如果使用注释,请确保以下注释的namespace属性与相应WSDL元素的namespace属性匹配:
@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元素之间匹配。
答案 2 :(得分:0)
我遇到了同样的错误而且我使用的是Soap1.1,我注意到Spring暴露了这个问题,并且我的客户端使用Spring WS和JAXB生成的默认情况下没有设置任何soapAction的@SoapAction注释,所以我不得不使用SoapActionCallback来设置WSDL中为该操作指定的SoapAction。
SoapActionCallback soapActionCallback = new SoapActionCallback("restituisciStatiMessaggioListRequestSA");
RestituisciStatiMessaggioListResponse response = (RestituisciStatiMessaggioListResponse) getWebServiceTemplate().marshalSendAndReceive(request,soapActionCallback);
答案 3 :(得分: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";