[英]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.
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)。
經過https://stackoverflow.com/questions/5981379/the-endpoint-reference-epr-for-the-operation-not-found-is
之后,我在標頭中添加了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>
問題是所需的標題格式不正確。 最初,我無法創建objectPayload,因此無法使用marshalSendAndReceive。 因此,為了創建適當的objectPayload,我使用wsdl從命令提示符下生成ObjectFactory,然后將所有創建的對象導入到項目中。 使用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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.