我在企业级使用以下 SOAP 请求
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
....
</soapenv:Header>
<soapenv:Body>
<UserRQ xmlns="http://www.user.org/USER/INFO" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
...
为了使用邮递员(左右(访问 API,我正在使用 WebServiceClient,目前正在将整个请求(包括标头和正文(传递到其中
应用程序客户端.java:
public class ApplicationClient extends WebServiceGatewaySupport {
//Envelope is the whole request
public ResponseEntity<String> getResults(Envelope envelope) {
WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("org.test.wsdl");
webServiceTemplate.setMarshaller(marshaller);
webServiceTemplate.setUnmarshaller(marshaller);
ResponseEntity<String> response = (ResponseEntity<String>) webServiceTemplate.marshalSendAndReceive("<URL>", envelope);
log.info(response.getStatusCode());
return response;
}
}
响应正确(获得 200 个响应代码(。无论如何可以通过 Web 服务客户端分别传递标头和正文吗?或者我们如何编组标题?
无法在 Web 服务调用中单独传递标头和正文。相反,有一些机制可以更改构造的消息,我们可以利用它来向标头添加身份验证,并将自定义元素添加到标头。为此,我们需要额外的 spring-ws 依赖项。
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-security</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-support</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.wss4j</groupId>
<artifactId>wss4j-parent</artifactId>
<version>2.3.0</version>
<type>pom</type>
</dependency>
然后创建一个执行身份验证的 bean(在这里,我给出了一个基本身份验证的示例(
@Bean
public Wss4jSecurityInterceptor wsSecurityInterceptor() {
Wss4jSecurityInterceptor wss4jSecurityInterceptor = new Wss4jSecurityInterceptor();
wss4jSecurityInterceptor.setSecurementActions(WSHandlerConstants.USERNAME_TOKEN);
wss4jSecurityInterceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
wss4jSecurityInterceptor.setSecurementUsername("abc");
wss4jSecurityInterceptor.setSecurementPassword("xxxxxxxx");
return wss4jSecurityInterceptor;
}
然后添加一个 WebServiceMessageCallback 实例来覆盖完成的消息,
webServiceTemplate.marshalSendAndReceive(url, request, new CustomWebServiceMessageCallback());
在 CustomWebServiceMessageCallback 中,通过使用以下方式,我们可以访问标头并对其进行修改。
@Override
public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
SoapMessage soapMessage = (SoapMessage) webServiceMessage;
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("<additional-header-element>HEADER_TEXT</additional-header-element>");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
}