在 SpringBoot 客户端中使用 WebServiceTemplate 调用 SOAP web 服务
SOAP web service call using WebServiceTemplate in SpringBoot client
我正在使用 WebServiceTemplate 调用 SOAP Web 服务。我可以使用 SOAP UI 通过以下输入调用服务并获得正确的响应。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://abc.hs.com/">
<soapenv:Header>
<version>1<version>
</soapenv:Header>
<soapenv:Body>
<abc:getUser>
<userId>pan</userId>
</abc:getUser>
</soapenv:Body>
</soapenv:Envelope>
但是使用WebserviceTemplate调用时如下:
public String getUser(String userId) {
List<String> detail = new ArrayList<String>();
try {
template = new WebServiceTemplate(marshaller);
String requestPayload = getXmlInput();// This is same xml I am sending using SOAPUI
StreamSource source = new StreamSource(new StringReader(requestPayload));
StreamResult result = new StreamResult(System.out);
template.sendSourceAndReceiveToResult("http://localhost:8080/HERSvc/InsService", source,
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) throws IOException {
TransportContext context = TransportContextHolder.getTransportContext();
HttpUrlConnection connection = (HttpUrlConnection) context.getConnection();
connection.addRequestHeader("Content-Type", "text/xml; charset=utf-8");
connection.addRequestHeader("soapAction", "");
}
},result);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
我在服务器端收到以下错误:E org.apache.axis2.engine.AxisEngine receive The endpoint reference (EPR) for the Operation not found is http://localhost:8080/HERSvc/InsService and the WSA Action = . If this EPR was previously reachable, please contact the server administrator.
客户端错误消息:出现意外错误(类型=内部服务器错误,状态=500)。
完成 后,我在 header
中添加了 Content-Type 和 soapAction
注意:我怀疑问题出在我的 requestPayload 格式不正确。它与 SOAP UI 中使用的字符串相同。似乎输入被转换为 Body
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<soapenv:Envelope xmlns:abc="http://abc.hs.com/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<version>1<version>
</soapenv:Header>
<soapenv:Body>
<abc:getUser>
<userId>pan</userId>
</abc:getUser>
</soapenv:Body>
</soapenv:Envelope>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
问题是 required Header 的格式不正确。最初我无法创建 objectPayload,因此无法使用 marshalSendAndReceive。因此,为了创建正确的 objectPayload,我使用我的 wsdl 从命令提示符生成 ObjectFactory,然后将创建的所有 objects 导入到项目中。使用 marshalSendAndReceive 如下
wsimport -d generated http://example.org/stock?wsdl
public User getUser(User user) {
try {
template = new WebServiceTemplate(marshaller);
JAXBElement<?> response = (JAXBElement<?>)template.marshalSendAndReceive("http://localhost:8080/HERSvc/InsService",
new ObjectFactory().createGetUser(user),
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
try {
SoapMessage soapMessage = (SoapMessage)message;
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("<version>1.0</version>");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
} catch (Exception e) {
e.printStackTrace();
}
}
}) ;
} catch (IOException e) {
e.printStackTrace();
}
GetUserResponse responseObject = (GetUserResponse)response.getValue();
User user = responseObject.getReturn();
}
我正在使用 WebServiceTemplate 调用 SOAP Web 服务。我可以使用 SOAP UI 通过以下输入调用服务并获得正确的响应。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://abc.hs.com/">
<soapenv:Header>
<version>1<version>
</soapenv:Header>
<soapenv:Body>
<abc:getUser>
<userId>pan</userId>
</abc:getUser>
</soapenv:Body>
</soapenv:Envelope>
但是使用WebserviceTemplate调用时如下:
public String getUser(String userId) {
List<String> detail = new ArrayList<String>();
try {
template = new WebServiceTemplate(marshaller);
String requestPayload = getXmlInput();// This is same xml I am sending using SOAPUI
StreamSource source = new StreamSource(new StringReader(requestPayload));
StreamResult result = new StreamResult(System.out);
template.sendSourceAndReceiveToResult("http://localhost:8080/HERSvc/InsService", source,
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) throws IOException {
TransportContext context = TransportContextHolder.getTransportContext();
HttpUrlConnection connection = (HttpUrlConnection) context.getConnection();
connection.addRequestHeader("Content-Type", "text/xml; charset=utf-8");
connection.addRequestHeader("soapAction", "");
}
},result);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
我在服务器端收到以下错误:E org.apache.axis2.engine.AxisEngine receive The endpoint reference (EPR) for the Operation not found is http://localhost:8080/HERSvc/InsService and the WSA Action = . If this EPR was previously reachable, please contact the server administrator.
客户端错误消息:出现意外错误(类型=内部服务器错误,状态=500)。
完成 后,我在 header
注意:我怀疑问题出在我的 requestPayload 格式不正确。它与 SOAP UI 中使用的字符串相同。似乎输入被转换为 Body
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<soapenv:Envelope xmlns:abc="http://abc.hs.com/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<version>1<version>
</soapenv:Header>
<soapenv:Body>
<abc:getUser>
<userId>pan</userId>
</abc:getUser>
</soapenv:Body>
</soapenv:Envelope>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
问题是 required Header 的格式不正确。最初我无法创建 objectPayload,因此无法使用 marshalSendAndReceive。因此,为了创建正确的 objectPayload,我使用我的 wsdl 从命令提示符生成 ObjectFactory,然后将创建的所有 objects 导入到项目中。使用 marshalSendAndReceive 如下
wsimport -d generated http://example.org/stock?wsdl
public User getUser(User user) {
try {
template = new WebServiceTemplate(marshaller);
JAXBElement<?> response = (JAXBElement<?>)template.marshalSendAndReceive("http://localhost:8080/HERSvc/InsService",
new ObjectFactory().createGetUser(user),
new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
try {
SoapMessage soapMessage = (SoapMessage)message;
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("<version>1.0</version>");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
} catch (Exception e) {
e.printStackTrace();
}
}
}) ;
} catch (IOException e) {
e.printStackTrace();
}
GetUserResponse responseObject = (GetUserResponse)response.getValue();
User user = responseObject.getReturn();
}