无法让CDI和JAX-RS在Glassfish中协同工作

问题描述 投票:2回答:1

我必须部署一个在Wildfly中成功运行到Glassfish的项目。除了在ResponseFilter中依赖注入对象之外,一切正常。该对象在REST资源处理程序中生成。我创建了一个简单的项目来演示这个问题。我在Stackoverflow上经历了几个答案。尝试了一切,似乎没有工作。

的;

<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"
    bean-discovery-mode="all">
</beans>

app.Java

@ApplicationPath("api")
public class App extends Application {
}

demo resource.Java

@Path("/demo")
public class DemoResource {

    @Produces
    private Pojo pojo;

    @GET
    public void demo() {
        pojo = new Pojo();
    }
}

PO Jo.Java

@Alternative
public class Pojo {
    protected String firstName;
    protected String lastName;
}

response filter.Java

@Provider
public class ResponseFilter implements ContainerResponseFilter {

    @Inject
    private Pojo injectedPojo;

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
        throws IOException {

        Instance<Pojo> instance = CDI.current().select(Pojo.class);
        Pojo uninjectedPojo = null;
        if (!instance.isUnsatisfied() && instance.get() != null)
            uninjectedPojo = instance.get();
        System.out.println("Injected POJO: " + injectedPojo);
        System.out.println("Uninjected POJO: " + uninjectedPojo);
    }
}

Wildfly日志文件中的输出是:

16:33:58,765 INFO  [stdout] (default task-3) Injected POJO: com.example.demo.Pojo@2092c78b
16:33:58,765 INFO  [stdout] (default task-3) Uninjected POJO: com.example.demo.Pojo@2092c78b

Glassfish日志中的输出是:

[2018-02-14T16:36:37.730+0545] [Payara 4.1] [INFO] [] [] [tid: _ThreadID=27 _ThreadName=http-thread-pool::http-listener-1(3)] [timeMillis: 1518605497730] [levelValue: 800] [[Injected POJO: null]]
[2018-02-14T16:36:37.730+0545] [Payara 4.1] [INFO] [] [] [tid: _ThreadID=27 _ThreadName=http-thread-pool::http-listener-1(3)] [timeMillis: 1518605497730] [levelValue: 800] [[Uninjected POJO: null]]
glassfish jax-rs cdi java-ee-7
1个回答
0
投票

<alternatives>声明添加到beans.xml

<beans ... >
    <alternatives>
        <class>[package name].Pojo</class>
    </alternatives>
</beans>

或者添加

@Priority(Interceptor.Priority.APPLICATION+100)

Pojo课堂的态度

© www.soinside.com 2019 - 2024. All rights reserved.