Has*_*ral 2 java spring soap header webservicetemplate
我有一个相当简单的案例,我试图将 HTTP 标头(不是 SOAP 标头)添加到我使用 Spring 的WebServiceTemplate.
我已经定义了ClientInterceptor我正在做的事情:
@Override
public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
try {
TransportContext context = TransportContextHolder.getTransportContext();
HttpComponentsConnection connection = (HttpComponentsConnection) context.getConnection();
connection.addRequestHeader("Authorization", String.format("Bearer %s", someAccessToken));
} catch (IOException exception) {
// Do nothing
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是我如何配置我的SomeClient延伸WebServiceConfigurationSupport:
@Bean
public SomeClient someClient() {
...
SomeClientImpl service = new SomeClientImpl();
service.setObjectFactory(new com.path.ObjectFactory());
service.setDefaultUri(someUri);
service.setMarshaller(marshaller);
service.setUnmarshaller(marshaller);
service.setxStreamMarshaller(xStreamMarshaller);
service.setInterceptors(new ClientInterceptor[]{wss4jSecurityInterceptor()});
service.setMessageSender(new HttpComponentsMessageSender());
service.setInterceptors(new ClientInterceptor[]{wss4jSecurityInterceptor(), addHttpHeaderInterceptor()});
return service;
}
@Bean
public ClientInterceptor addHttpHeaderInterceptor() {
return new AddHttpHeaderInterceptor(someAccessToken);
}
@Bean
public Wss4jSecurityInterceptor wss4jSecurityInterceptor() {
Wss4jSecurityInterceptor interceptor = new Wss4jSecurityInterceptor();
interceptor.setSecurementActions(securementAction);
interceptor.setSecurementUsername(securementUsername);
interceptor.setSecurementPassword(securementPassword);
interceptor.setSecurementPasswordType(WSConstants.PW_TEXT);
interceptor.setSecurementMustUnderstand(false);
return interceptor;
}
Run Code Online (Sandbox Code Playgroud)
但是Authorization没有添加标题。我也尝试过CustomMessageCallback:
public class CustomMessageCallback implements WebServiceMessageCallback {
private String headerKey;
private String headerValue;
public CustomMessageCallback(String headerKey, String headerValue) {
this.headerKey = headerKey;
this.headerValue = headerValue;
}
@Override
public void doWithMessage(WebServiceMessage webServiceMessage) throws IOException, TransformerException {
TransportContext context = TransportContextHolder.getTransportContext();
HttpComponentsConnection conn = (HttpComponentsConnection) context.getConnection();
HttpPost post = conn.getHttpPost();
post.addHeader(headerKey, headerValue);
}
}
Run Code Online (Sandbox Code Playgroud)
但它似乎也不起作用。我做错了什么,为什么Authorization没有添加标题?谢谢!
使用HeadersAwareSenderWebServiceConnection接口而不是实际的底层连接。
TransportContext context = TransportContextHolder.getTransportContext();
HeadersAwareSenderWebServiceConnection connection = (HeadersAwareSenderWebServiceConnection) context.getConnection();
connection.addRequestHeader("Authorization", String.format("Bearer %s", "********"));
Run Code Online (Sandbox Code Playgroud)
现在,如果您升级/切换 HTTP 库,则无需更改此代码。
回答关于你做错了什么的问题是你投错了班。是的,您正在使用的类已被弃用,但它是您正在使用的库的一部分,您不能在不更改底层 HTTP 库的情况下将其转换为不同的类。
| 归档时间: |
|
| 查看次数: |
6126 次 |
| 最近记录: |