我有两个用JAX-WS实现的Web服务:RequestService
和ExtendedRequestService
。ExtendedRequestService
调用RequestService
并将其他数据添加到响应中。
调用RequestService
时,将注入WebServiceContext
,但是从ExtendedRequestService
调用时,其为null,并且代码将引发NullPointerException
。
如何正确初始化?
这就是我从RequestService
调用ExtendedRequestService
的方式:
RequestServiceImpl requestService = new RequestServiceImpl();
RequestServiceResponse requestServiceResponse = requestService.performRequest(requestServiceRequest);
最佳答案
不知道这是否是最好的解决方案,但这似乎正常工作:
在您的服务实现中这样声明WebServiceContext
:
private WebServiceContext wsContext;
@Resource
public void setContext(WebServiceContext context) {
this.wsContext = context;
}
然后,当您需要从
RequestService
调用ExtendedRequestService
时,请使用:RequestServiceImpl requestService = new RequestServiceImpl();
requestService.setContext(wsContext); //The WebServiceContext of your ExtendedRequestService class
RequestServiceResponse requestServiceResponse = requestService.performRequest(requestServiceRequest);