我有两个使用JAX-WS实现的Web服务:RequestService
和ExtendedRequestService
。
ExtendedRequestService
调用RequestService
并向响应中添加其他数据。
在调用RequestService
时会注入WebServiceContext
,但是当从ExtendedRequestService
调用它时,它为空,代码会抛出NullPointerException
。
如何正确初始化它?
这是我从RequestService
致电ExtendedRequestService
的方式:
RequestServiceImpl requestService = new RequestServiceImpl();
RequestServiceResponse requestServiceResponse = requestService.performRequest(requestServiceRequest);
答案 0 :(得分:0)
不确定这是否是最佳解决方案,但这似乎可以正常运行:
在您的服务实施中声明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);