我使用HTTP传输遵循http://docs.spring.io/spring-ws/sites/1.5/reference/html/client.html中提到的步骤。我的代码如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.ws.client.core.WebServiceTemplate;
import java.io.StringReader;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.transport.WebServiceMessageSender;
@Component
public class blahClient {
@Autowired
private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
public void setDefaultUri(String defaultUri) {
webServiceTemplate.setDefaultUri(defaultUri);
}
// send to an explicit URI
public void customSendAndReceive() {
StreamSource source = new StreamSource(new StringReader(MESSAGE));
StreamResult result = new StreamResult(System.out);
webServiceTemplate.sendSourceAndReceiveToResult(webServiceTemplate.getDefaultUri(),source, result);
System.out.println(result.toString());
}
private static final String MESSAGE = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.blah.internal.blah.com\"><soapenv:Header/><soapenv:Body><ser:getblahData><request><caller_id>loginapp</caller_id><content>1067857</content><locale></locale><output>XML, Formatted</output><security_token>vz37ahzcwzakua3z9q2zno66e4xy6wyg8oxh6xn7bordkl</security_token></request></ser:getblahData></soapenv:Body></soapenv:Envelope>";
public static void main(String[] args) {
IntactwsClient blahClient = new blahClient();
blahClient .setDefaultUri("http://blah.com/blah/blahService");
blahClient .customSendAndReceive();
}
}
它给了我这个例外:
Exception in thread "main" org.springframework.ws.soap.client.SoapFaultClientException: org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
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)
at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:421)
at com.blah.internal.blah.service.IntactwsClient.customSendAndReceive(IntactwsClient.java:35)
at com.blah.internal.blah.service.IntactwsClient.main(IntactwsClient.java:59)
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:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我已经使用相同的XML消息检查了来自SoapUI的SOAP调用,并且它工作正常。我无法诊断出现此错误的原因。我不需要创建一个类并使用从响应中编组的对象,因为我只需要从响应中解析一个值。我希望WebServiceTemplate.sendSourceAndReceiveToResult可以像Soap RestTemplate一样工作,并产生一个可以解析的响应。
任何帮助将不胜感激。
答案 0 :(得分:0)
StreamResult result = new StreamResult(System.out);
System.out.println(result.toString());
我认为这两行是造成例外的原因。阅读here
您正在尝试将StreamResult对象直接转换为字符串。 相反,您应该将输出流(System.out)用于字符串。
试试这个,
System.out.toString();
System.out.println(result.toString()); //Remove this line