简体   繁体   English

Jersey 1 @Inject 迁移到 Jersey 2 停止工作

[英]Jersey 1 @Inject migrated to Jersey 2 stopped working

I am doing a Jersey 1 to Jersey 2 migration of my system.我正在对我的系统进行 Jersey 1 到 Jersey 2 的迁移。 In my code I had @Inject annotation from com.sun.jersey.spi.inject.Inject and @Singleton from com.sun.jersey.spi.resource.Singleton . In my code I had @Inject annotation from com.sun.jersey.spi.inject.Inject and @Singleton from com.sun.jersey.spi.resource.Singleton . I've changed these to javax.inject.Inject and javax.inject.Singleton .我已将这些更改为javax.inject.Injectjavax.inject.Singleton

Since this change I am getting errors while injecting any object annotated with it.由于此更改,我在注入任何带有注释的 object 时遇到错误。 My error is this我的错误是这个

javax.servlet.ServletException: A MultiException has 4 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=APIConnectorHandler,parent=BarcodeSearchClient,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1090224052)
2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=APIConnectorHandler,parent=RSearchClient,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1247004825)
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.search.barcode.BarcodeSearchClient errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on com.search.barcode.BarcodeSearchClient

    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:423)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    com.filter.MetricsEmittingFilter.doFilter(MetricsEmittingFilter.java:89)

The APIConnectorHandler is Injected both in RSearchClient and BarcodeSearchClient . APIConnectorHandlerRSearchClientBarcodeSearchClient中都被注入。 I first called the endpoint hitting RSearchClient in the flow and then BarcodeSearchClient because of which RSearchClient is also shown as exception #2.我首先调用了在流程中击中 RSearchClient 的端点,然后调用了 BarcodeSearchClient,因为 RSearchClient 也显示为异常 #2。

My web.xml looks like this我的 web.xml 看起来像这样

<?xml version="1.0"?>
<!--
When modifying this file, DO NOT FORGET to also modify workspace-web.xml
if necessary so that "brazil-build server" will work.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <context-param>
        <description>Spring Expression Language Support</description>
        <param-name>springJspExpressionSupport</param-name>
        <param-value>false</param-value>
    </context-param>
    <listener>
        <listener-class>com.abc.listener.ContextListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>JerseyServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>
                com.search, com.fasterxml.jackson.jaxrs.json
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JerseyServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <filter-mapping>
        <filter-name>MetricsEmittingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

A few things i did find what that Hk2 is unable to find the beans and stuff but what i'm unable to understand is how did it work in Jersey 1. Where was Jersey 1 @Inject getting the beans from?我确实找到了一些 Hk2 无法找到豆子和东西的东西,但我无法理解的是它在 Jersey 1 中是如何工作的。Jersey 1 @Inject从哪里得到豆子?

PS. PS。 This is a super legacy code and i don't really know why but it has spring integration but none of the objects are created as beans.这是一个超级遗留代码,我真的不知道为什么,但它具有 spring 集成,但没有一个对象被创建为 bean。 They are just initialized with new in multiple places.它们只是在多个地方用 new 初始化。 Spring is used to initialize only region specific beans. Spring 仅用于初始化特定于区域的 bean。

Got it working.得到它的工作。 I had to create a binder class and app class and link it in web.xml.我必须创建一个活页夹 class 和应用程序 class 并将其链接到 web.Z0F635D0E0F38724FFF8B581C7.A3 The binder class needed to have binding of all the classes i'm injecting via @Inject and their dependencies.绑定器 class 需要绑定我通过 @Inject 注入的所有类及其依赖项。

Followed https://www.appsdeveloperblog.com/dependency-injection-hk2-jersey-jax-rs/ 关注https://www.appsdeveloperblog.com/dependency-injection-hk2-jersey-jax-rs/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM