ibm-mobilefirst - CDI 在简单的适配器中不起作用

标签 ibm-mobilefirst cdi websphere-liberty mobilefirst-adapters open-liberty

我已将 CDI 功能添加到 server.xml 文件 <feature>cdi-1.2</feature> .

我的 maven 模块包含 beans.xml 里面<module_name>/src/main/resources/META-INF文件夹。

这是 beans.xml 内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                           http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.1" bean-discovery-mode="all">
</beans>

但是当我使用 @Inject注释它不起作用,我的 bean 总是 null .

代码:
package ch.webapp.presentation;

...

@Path("/test/")
public class MyController {
    @Inject
    private MyService myService;

    @GET
    @Path("/foo/{count}")
    @OAuthSecurity(scope = "login")
    @Produces("application/json")
    public Response news(@PathParam("count") int count) {
        return Response
                .ok(myService.getBar(count))
                .build();
    }
}

编辑:

那是我的 bean
package ch.webapp.service;

...

@RequestScoped
public class MyService {
    public String getBar(int count) {
        return "foo";
    }
}

我通过扩展 MFPJAXRSApplication 来初始化 jax-rs类(class)
package ch.webapp;

...

public class AccountApplication extends MFPJAXRSApplication {
    @Override
    protected void init() throws Exception {
    }

    @Override
    protected void destroy() throws Exception {
    }

    @Override
    protected String getPackageToScan() {
        return getClass().getPackage().getName();
    }
}

环境详情:
Launching mfp (WebSphere Application Server 8.5.5.8/wlp-1.0.11.cl50820151201-1942) on Java HotSpot(TM) 64-Bit Server VM, version 1.8.0_172-b11 (en_CH)

Console Product version: 8.0.0.00-20180717-175523

怎么了?

最佳答案

首先,除非您对它们进行适当的注释,否则 websphere jax-rs 实现似乎不会自动集成 jax-rs 资源。

通过适当注释将 jax-rs 置于 CDI 托管上下文中

@Path("/test/")
@javax.enterprise.context.RequestScoped
public class MyController {

    @Inject
    private MyService myService;

    @GET
    @Path("/foo/{count}")
    @OAuthSecurity(scope = "login")
    @Produces("application/json")
    public Response news(@PathParam("count") int count) {
        return Response
                .ok(myService.getBar(count))
                .build();
    }

}

还要确保用于您的服务的注释是@javax.enterprise.context.RequestScoped

关于ibm-mobilefirst - CDI 在简单的适配器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51879492/

相关文章:

java - 无法在 IBM bluemix 上 @Inject

ibm-mobilefirst - Worklight http 适配器问题

tomcat7 - 为 Google Cloud Messaging 设置服务器端和客户端

java - CDI - 事件观察者

jakarta-ee - 如何在Web项目中使用来自Netbeans ejb项目的EJB?

重新部署 EJB 后的 javax.ejb.NoSuchEJBException

java - JAXB 内部实现接口(interface) com.sun.xml.bind.namespacePrefixMapper 的类加载器问题

java - WebSphere 自由类默认加载 PARENT LAST

javascript - 如何使用javascript从网站获取数据表?

ibm-mobilefirst - 由 : com. ibm.db2.jcc.am.SqlSyntaxErrorException : DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884 引起