我有两个用JAX-WS实现的Web服务:RequestServiceExtendedRequestService

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);

09-25 21:26