【问题标题】:Why CDI not working?为什么 CDI 不工作?
【发布时间】:2013-11-09 13:48:02
【问题描述】:

我有下一个休息服务:

@ApplicationPath("geo")
@Path("weather")
public class MainResource extends Application {

    @Inject
    private MainDep dep;

    @GET
    public String printGotIt() {
        return "Got it!";
    }

    @GET
    @Path("propaganda")
    public String printPropaganda() {
        return dep.printPropaganda();
    }
}

MainDep 依赖代码:

public class MainDep {
    public String printPropaganda() {
        return "Interesting enterprise";
    }
}

当我尝试在以下 url 上使用资源时:host:port/root/geo/weather GlassFish 抛出了 javax.servlet.ServletException:

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
`javax.servlet.ServletException: Servlet.init() for servlet com.app.weather.rs.MainResource threw exception
root cause`
A MultiException has 1 exceptions.  They are:
    1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MainDep,parent=MainResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,22064320)
    root cause
    org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MainDep,parent=MainResource,qualifiers={}),position=-1,optional=false

【问题讨论】:

    标签: java glassfish cdi jersey-2.0


    【解决方案1】:

    这里的问题是您将 JAX-RS 应用程序 + JAX-RS 资源类混合在一个类中,并且在此基础上您将 CDI 注入添加到混合中。

    尝试将 JAX-RS 应用程序与 JAX-RS 资源分开,例如:

    @ApplicationPath("geo")
    public class MainApplication extends Application {
    
        @Override
        public Set<Class<?>> getClasses() {
            final Set<Class<?>> classes = new HashSet<Class<?>>();
            classes.add(MainResource.class);
            return classes;
        }
    }
    

    @Path("weather")
    public class MainResource {
    
        @Inject
        private MainDep dep;
    
        @GET
        public String printGotIt() {
            return "Got it!";
        }
    
        @GET
        @Path("propaganda")
        public String printPropaganda() {
            return dep.printPropaganda();
        }
    }
    

    【讨论】:

    • public Set&lt;Class&lt;?&gt;&gt; getClasses() 不需要。谢谢。
    【解决方案2】:

    您需要将@RequestScoped 添加到您的班级。

    【讨论】:

    • 如果我将 @RequestScoped 添加到 MainResource 类,我无法将我的应用程序部署到 glassfish 4。错误文本:An error has occurred Error occurred during deployment: Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [Ref&lt;ContainerRequest&gt;] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref&lt;ContainerRequest&gt;, ProcessingProviders)]. Please see server.log for more details.
    • 我认为@RequestScoped 默认是不需要的
    • CDI 中没有默认值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2018-02-23
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2013-06-01
    相关资源
    最近更新 更多