WebServiceContext保持为null

时间:2016-03-15 08:45:58

标签: java java-ee jax-ws

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

ExtendedRequestService调用RequestService并向响应中添加其他数据。 在调用RequestService时会注入WebServiceContext,但是当从ExtendedRequestService调用它时,它为空,代码会抛出NullPointerException

如何正确初始化它?

这是我从RequestService致电ExtendedRequestService的方式:

RequestServiceImpl requestService = new RequestServiceImpl();
RequestServiceResponse requestServiceResponse = requestService.performRequest(requestServiceRequest);

1 个答案:

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