首先,我将解释该项目。它是一个集成的Web Service,它侦听请求并在内部使用另一个Web Service并处理其响应,以便获得所需的答案。我需要向“内部” Web服务的每个请求添加一个自定义标头。我已经检查过使用下一个配置,该处理程序是单例类。我需要对“内部服务”的每个新请求都创建一个新实例。我检查过,有时请求“ A”的标头使用请求“ B”的值。我正在初始化第一个“内部”请求之前的标头值(我需要在没有任何肥皂标头的情况下调用“内部” Web服务的方法,然后使用第一个响应中包含的值进行设置)。有什么想法可以使这项工作吗?
谢谢
//ConfigurationClass
@Bean(name = "internalService")
@Scope(scopeName="prototype")
public JaxWsPortProxyFactoryBean internalService() {
JaxWsPortProxyFactoryBean bean = new JaxWsPortProxyFactoryBean();
try {
bean.setServiceInterface(internalService.class);
bean.setWsdlDocumentUrl(new URL("https://localhost/internalService.svc?wsdl" ));
bean.setNamespaceUri( "http://schemas.internalService.com/2019/04/");
bean.setServiceName("InternalService");
bean.setPortName("InternalServicePort");
bean.setEndpointAddress("https://localhost/internalService.svc");
bean.setHandlerResolver(wsHandlerResolver());
} catch (MalformedURLException e) {
e.printStackTrace();
}
return bean;
}
@Bean(name = "wsHandlerResolver")
public WebServiceHandlerResolver wsHandlerResolver() {
WebServiceHandlerResolver wshandlerResolver = new WebServiceHandlerResolver();
List handlers = new ArrayList();
handlers.add(webServiceHandler());
wshandlerResolver.setHandlers(handlers);
return wshandlerResolver;
}
@Bean(name = "webServiceHandler")
public WebServiceHandler webServiceHandler() {
WebServiceHandler webServiceHandler = new WebServiceHandler();
return webServiceHandler;
}
//HandlerResolver class
public class WebServiceHandlerResolver implements HandlerResolver {
private List<Handler> handlers;
public List<Handler> getHandlerChain(PortInfo portInfo) {
return handlers;
}
public void setHandlers(List<Handler> handlers) {
this.handlers = handlers;
}
}
//Handler class
public class WebServiceHandler implements SOAPHandler<SOAPMessageContext> {
private String user;
private String pass;
private String source;
@Override
public boolean handleMessage(SOAPMessageContext context) {
//THIS IS WHERE I ADD THE VALUES
}
}
答案 0 :(得分:1)
在WebServiceHandler中使用java.lang.ThreadLocal<YourPropertiesHolder>
变量来存储和访问属性。这样,来自您服务的不同请求的属性将不会发生冲突。
class YourPropertiesHolder {
String user;
String pass;
String source;
}
答案 1 :(得分:0)
最后我做到了。这只是Handler类中范围请求的问题,我缺少proxyMode选项。
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)