【发布时间】:2021-03-19 19:03:08
【问题描述】:
一个SOAP网络服务,接受以下格式的请求 -
<?xml version = "1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
xmlns:ns="http://...." xmlns:ns1="http://...." xmlns:ns2="http://...."
xmlns:ns3="http://....">
<SOAP-ENV:Header>
<ns:EMContext>
<messageId>1</messageId>
<refToMessageId>ABC123</refToMessageId>
<session>
<sessionId>3</sessionId>
<sessionSequenceNumber>2021-02-24T00:00:00.000+5:00</sessionSequenceNumber>
</session>
<invokerRef>CRS</invokerRef>
</ns:EMContext>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:getEmployee>
<ns:empId>111</ns:empId>
</ns1:getEmployee>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
当尝试使用 JAXB2 向它发出 SOAP 请求时,它给出了org.springframework.ws.soap.client.SoapFaultClientException: EMContext Header is missing
我正在使用
pring-boot-starter
spring-boot-starter-web-services
org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.14.0
和
客户 -
public class MyClient extends WebServiceGatewaySupport {
public GetEmployeeResponse getEmployee(String url, Object request){
GetEmployeeResponse res = (GetEmployeeResponse) getWebServiceTemplate().marshalSendAndReceive(url, request);
return res;
}
}
配置-
@Configuration
public class EmpConfig {
@Bean
public Jaxb2Marshaller marshaller(){
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setContextPath("com.crsardar.java.soap.client.request");
return jaxb2Marshaller;
}
@Bean
public MyClient getClient(Jaxb2Marshaller jaxb2Marshaller){
MyClient myClient = new MyClient();
myClient.setDefaultUri("http://localhost:8080/ws");
myClient.setMarshaller(jaxb2Marshaller);
myClient.setUnmarshaller(jaxb2Marshaller);
return myClient;
}
}
应用程序-
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
CommandLineRunner lookup(MyClient myClient){
return args -> {
GetEmployeeRequest getEmployeeRequest = new GetEmployeeRequest();
getEmployeeRequest.setId(1);
GetEmployeeResponse employee = myClient.getEmployee("http://localhost:8080/ws", getEmployeeRequest);
System.out.println("Response = " + employee.getEmployeeDetails().getName());
};
}
}
如何将 EMContext Header 添加到 SOAP 请求中?
【问题讨论】:
-
您是通过soapui 拨打电话吗?从哪里获得请求 sn-p 是来自另一个环境吗?
-
@NirajJha 是的,它来自不同的环境。这是一个非常古老且非常大的项目,WSDL 没有得到适当的维护。如果我在 SoapUI 中使用上述输入,我会得到正确的结果。如果我使用 java.net.HttpURLConnection 并将完整的输入(带有标题,如上所述)作为字节写入 OutputStream 并从 InputStream 读取(取自 java.net.HttpURLConnection),那么也会得到正确的响应。尝试使用 JAXB2 复制相同的东西,这会给出“EMContext Header is missing”
-
由 JAXB2 生成的类,使用 WSDL,没有任何称为 EMContext 的东西,也没有任何类上的 EMContext 的 getter 或 setter。正如我所提到的 - 这是一个非常古老且非常大的项目,WSDL 维护不正确。
标签: java spring spring-boot jaxb maven-jaxb2-plugin