JAX-WS 和 CDI 未在 WAS Liberty Profile 8.5.5.6 上协同工作

JAX-WS and CDI not working together on WAS Liberty Profile 8.5.5.6

当我尝试组合 JAX-WS Web 服务端点 class 和简单的 CDI 注入时,我遇到了奇怪的行为。当我尝试将对象注入 WebService 实现 class 时,从未调用注入对象的 PostConstruct 方法。事实上,classes 构造函数也未被调用。

这是我的 JAX-WS 实现 class 和注入点:

@WebService(serviceName="eBusinessWebService")
public class eBusinessWebServiceImpl
{

    @WebMethod
    public SubmissionValidationResults xmlValidation(String xml, String submissionType, String schemaVersion)
            throws SOAPException
    {          
        // Validate schema
        SubmissionValidationResults results = fileSubmissionServiceHandler.validateXML(xml, submissionType,
                schemaVersion);
        
        return results;
    }

    @Inject
    private FileSubmissionServiceHandler fileSubmissionServiceHandler;

    @Inject
    private BRSubmissionService brSubmissionService;
}

这是我注入的 class,FileSubmissionServiceHandler:

public class FileSubmissionServiceHandler
{
    public FileSubmissionServiceHandler()
    {
        System.out.println("Constructor being called on FileSubmissionServiceHandler");
    }

    @PostConstruct
    public void init()
    {
        final String webserviceURL = "https://hostname/FileSubmissionService/FileSubmissionService.svc";
        final String username = "username";
        final String password = "password";
        
        this.webservice = new BasicHttpsBinding_IFileSubmissionServiceProxy(username, password);
        Descriptor desc = webservice._getDescriptor();
        desc.setEndpoint(webserviceURL);
    }
    
    public SubmissionValidationResults validateXML(String xml, String submissionType,
            String schemaVersion) throws WebServiceException
    {
        SubmissionValidationResults results = null;
        FormType type = FormType.getByName(submissionType);
        String submissionTypeCode = type.getCode();

        try
        {
            results = this.webservice.validateXmlFile(xml, submissionTypeCode, schemaVersion);
            
        }
        catch (Exception e)
        {
            logger.error("Internal FileSubmissionService threw an exception", e);
            throw e;
        }

        return convertSubmissionValidationResults(results);
    }

    private BasicHttpsBinding_IFileSubmissionServiceProxy webservice;
}

我被要求 post 我的服务器 XML(由于同时有两份 Liberty 配置文件 运行 而覆盖端口设置):

<server description="new server">

<!-- Enable features -->
<featureManager>
    <feature>jaxws-2.2</feature>
    <feature>servlet-3.1</feature>
    <feature>cdi-1.2</feature>
</featureManager>

<!--For a user registry configuration, configure your user registry. For 
    example, configure a basic user registry using the basicRegistry element. 
    Specify your own user name below in the name attribute of the user element. 
    For the password, generate an encoded password using bin/securityUtility 
    encode and add it in the password attribute of the user element. Then uncomment 
    the user element. -->
<basicRegistry id="basic" realm="BasicRealm">
    <user name="wasadmin" password="{xor}KD4sPjsyNjE=" />
</basicRegistry>

<keyStore password="{xor}KD4sPjsyNjE=" />

<!-- To access this server from a remote client add a host attribute to 
    the following element, e.g. host="*" -->
<httpEndpoint host="*" httpPort="9090" httpsPort="9445"
    id="defaultHttpEndpoint" />

<wasJmsEndpoint wasJmsPort="7277" wasJmsSSLPort="7287" />

<iiopEndpoint host="localhost" id="defaultIiopEndpoint"
    iiopPort="2814">
    <iiopsOptions iiopsPort="2815" />
</iiopEndpoint>

<applicationMonitor updateTrigger="mbean" />

<enterpriseApplication id="eBusinessWebService_EAR"
    location="eBusinessWebService_EAR.ear" name="eBusinessWebService_EAR" />

和日志:

Launching defaultServer (WebSphere Application Server 8.5.5.6/wlp-1.0.9.cl50620150610-1749) on Java HotSpot(TM) 64-Bit Server VM, version 1.7.0_79-b15 (en_US)
[AUDIT   ] CWWKE0001I: The server defaultServer has been launched.
[AUDIT   ] CWWKE0100I: This product is licensed for development, and limited production use. The full license terms can be viewed here: https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/license/base_ilan/ilan/8.5.5.6/lafiles/en.html
[AUDIT   ] CWWKZ0058I: Monitoring dropins for applications. 
[WARNING ] CWNEN0047W: Resource annotations on the fields of the xxx.important.not.external.service.eBusinessWebServiceImpl class will be ignored. The annotations could not be obtained because of the exception : java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContextAware
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:9090/eBusinessWebService/
[AUDIT   ] CWWKZ0001I: Application eBusinessWebService_EAR started in 3.011 seconds.
[AUDIT   ] CWWKF0012I: The server installed the following features: [jaxws-2.2, cdi-1.2, servlet-3.1, jndi-1.0, javaMail-1.5, jaxb-2.2].
[AUDIT   ] CWWKF0011I: The server defaultServer is ready to run a smarter planet.
20-01-2016 - 10:02:47 - INFO  (eBusinessWebServiceImpl.java:31) - --- Validating Incomming Form XML ---
20-01-2016 - 10:02:47 - INFO  (eBusinessWebServiceImpl.java:33) - Received Payload: XML [Hello]
20-01-2016 - 10:02:47 - INFO  (eBusinessWebServiceImpl.java:34) - SubmissionType [from the]
20-01-2016 - 10:02:47 - INFO  (eBusinessWebServiceImpl.java:35) - SchemaVersion [other side]
[WARNING ] Application {http://service.external.not.important.xxx/}eBusinessWebService#{http://service.external.not.important.xxx/}xmlValidation has thrown exception, unwinding now
java.lang.NullPointerException

我已经删除了每个class的一些不太相关的细节,但基本操作是相同的。当我尝试访问 fileSubmissionServiceHandler 对象的“validateXML”方法时,抛出一个空指针异常,并且我从未在我的 FileSubmissionServiceHandler [=40] 中看到 postConstruct 或构造函数方法的输出=].使用调试器,这些方法永远不会到达。

到目前为止我检查过的东西:

  1. 我的 WEB-INF 文件夹中有一个空 beans.xml 文件
  2. 我在 server.xml 中包含 javaee-7.0 功能,其中包括 jax-ws 和 cdi
  3. 我还尝试将应用程序范围和请求范围添加到 FileSubmissionServiceHandler,但没有任何效果。

有没有人知道为什么这行不通?

看起来由于某种原因没有执行注入。结果,构造函数和 PostConstruct 没有被调用,然后您遇到了 NPE。可以附上您的申请,以便我进一步查看吗?

因为你没有粘贴日志,也没有粘贴配置文件,server.xml等。我试图在 8.5.5.6 上产生问题,但我发现我可以进行注入并调用 post构造方法。

我的测试用例和你的类似。唯一的区别是我没有使用 javaee-7.0 功能,因为它需要在 server.xml 中进行更多配置,但只包含以下功能:

jaxws-2.2、servlet-3.1、cdi-1.2

请试一试。

如果还是不行,请确认不注入也能正常使用。您最好粘贴日志和完整的 server.xml,因为这些对问题确定很重要。

好的,我终于确定了问题的原因。

CDI 正在加载名为 BRSubmissionService 的对象。 CDI 加载被 spring 异常中断。并且由于 CDI 无法完成,剩余的 CDI 注入不会发生并且我的 FileSubmissionService 对象未加载,导致我的空指针异常。

解决方案是修复位于另一个 jar 中的 BRSubmissionService 对象内的 spring 错误,不幸的是我从另一个开发人员那里继承了这个错误。生活就是这样。