本文介绍了CDI / Weld无法执行RESTEasy资源的构造函数注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在AppEngine上将RESTEasy与Weld结合起来,但是在进行构造函数注入时遇到了麻烦。

I'm trying to combine RESTEasy with Weld on AppEngine but having troubles to do constructor injection.

我添加了RESTEasy CdiInjectorFactory上下文参数和Weld servlet

I've added the RESTEasy CdiInjectorFactory context param and the Weld servlet listener.

我的RESTEasy应用程序类如下:

My RESTEasy application class looks like:

public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(CustomerResource.class);
        return classes;
    }
}

和CustomerResource:

and the CustomerResource:

@Path("/rest/app/customers")
public class CustomerResource {

    private final CustomerService customerService;

    @Inject
    public CustomerResource(CustomerService customerService) {
        this.customerService = customerService;
    }

    ..
}

CustomerService是一种简单的服务,例如:

The CustomerService is a simple service like:

public class CustomerService {

    public List<Customer> getCustomers() {
        ..
    }
}

何时我启动了在日志中看到的服务器:

When I start up the server I see in the log:

[INFO] 2014-01-30 21:34:53 INFO  org.jboss.weld.Version:146 - WELD-000900: 2.1.2 (Final)
[INFO] 2014-01-30 21:34:53 WARN  org.jboss.weld.environment.servlet.Listener:137 - @Resource injection not available in simple beans
[INFO] 2014-01-30 21:34:54 INFO  org.jboss.weld.Bootstrap:199 - WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Discovered CDI bean which is javax.ws.rs.core.Application subclass com.mycomp.config.MyApplication.
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Bean class com.mycomp.config.MyApplication does not have the scope defined. Binding to @javax.enterprise.context.ApplicationScoped().
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Discovered CDI bean which is a JAX-RS resource com.mycomp.rest.app.CustomerResource.
[INFO] 2014-01-30 21:34:55 DEBUG org.jboss.resteasy.cdi.ResteasyCdiExtension:68 - Bean class com.mycomp.rest.app.CustomerResource does not have the scope defined. Binding to @javax.enterprise.context.RequestScoped().
[INFO] 2014-01-30 21:34:55 INFO  org.jboss.weld.environment.servlet.Listener:147 - No supported servlet container detected, CDI injection will NOT be available in Servlets, Filters or Listeners
[INFO] 2014-01-30 21:34:56 WARN  org.jboss.weld.Interceptor:47 - WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
[INFO] 2014-01-30 21:34:56 WARN  org.jboss.weld.Interceptor:47 - WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled

从此日志记录中,我可以看到发现了我的RESTEasy应用程序和资源类。请注意,我看不到有关CustomerService的任何信息。

From this logging I can see that my RESTEasy application and the resources class are discovered. Note that I don't see any mention about the CustomerService.

在浏览器中访问资源时,我得到:

When I access my resource in the browser I get:

[INFO] 2014-01-30 21:47:26 DEBUG org.jboss.resteasy.cdi.CdiInjectorFactory:68 - Using CdiConstructorInjector for class class com.mycomp.config.MyApplication.
[INFO] 2014-01-30 21:47:26 DEBUG org.jboss.resteasy.cdi.CdiConstructorInjector:68 - Beans found for class com.mycomp.config.MyApplication : [Managed Bean [class com.mycomp.config.MyApplication] with qualifiers [@Any @Default]]
[INFO] 2014-01-30 21:47:26 INFO  org.jboss.resteasy.spi.ResteasyDeployment:82 - Deploying javax.ws.rs.core.Application: class com.mycomp.config.MyApplication$Proxy$_$$_WeldClientProxy
[INFO] 2014-01-30 21:47:26 INFO  org.jboss.resteasy.spi.ResteasyDeployment:82 - Adding class resource com.mycomp.rest.app.CustomerResource from Application class com.mycomp.config.MyApplication$Proxy$_$$_WeldClientProxy
[INFO] 2014-01-30 21:47:26 INFO  org.jboss.resteasy.spi.ResteasyDeployment:82 - Adding class resource com.mycomp.rest.HeartbeatResource from Application class com.mycomp.config.MyApplication$Proxy$_$$_WeldClientProxy
[INFO] Jan 30, 2014 9:47:26 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
[INFO] SEVERE: javax.servlet.ServletContext log: unavailable
[INFO] java.lang.RuntimeException: Could not find constructor for class: com.mycomp.rest.app.CustomerResource

RESTEasy是否正在尝试创建新的CustomerResource实例而不是从Weld中选择它?

Is RESTEasy trying to create a new CustomerResource instance and not picking it from Weld?

我还尝试了现场注入(

I also tried with field injection (instead of constructor injection) and then I can see the CustomerService is nicely injected in the CustomerResource.

我只更喜欢构造函数注入,并且想知道RESTEasy资源类在何时支持此功能吗?

I only prefer constructor injection and wonder if this is supported in RESTEasy resource classes when using Weld?

推荐答案

JAX-RS要求您的其余资源使用no arg构造函数。您可以有其他构造函数,但还需要有一个无参数的构造函数。

JAX-RS requires a no arg constructor for your rest resources. You can have other constructors, but you need to also have a no-arg constructor.

这篇关于CDI / Weld无法执行RESTEasy资源的构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 07:02