jersey - 无法解决这个问题 UnsatisfiedDependencyException : There was no object available for injection at SystemInjecteeImpl

标签 jersey jersey-2.0 hk2 tomcat9 java-ee-8

我已经尝试了很多来解决这个问题,但不能。我发现,如果我们使用“abstractBinder”,那么这可以解决,但是一旦我的 Binder 就位,我就开始出现 404 错误。

UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl

请帮忙

我的资源:
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.walmart.services.helpers.IUserService;
import com.walmart.services.helpers.ServicesTest;

@Path("/sayHello")
public class ControllerTest {

    @Inject
    private IUserService service;

    @Inject
    private ServicesTest service2;

    @GET
    @Path("/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public String method(@PathParam("name") String msg) {
        return service.method() + " msg";
    }

    @GET
    @Path("/v2/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public String method2(@PathParam("name") String msg) {
        return service2.method() + " msg";
    }
}

我的资源配置文件:
@ApplicationPath("/rest/*")
public class ResourceConfiguration extends ResourceConfig {

    public ResourceConfiguration() {
        //register(new MyBinder());
        this.packages(true, "com.walmart.services.*");
    }

}

我的活页夹 [如果到位]
public class MyBinder extends AbstractBinder
{

    @Override
    protected void configure() {
        // TODO Auto-generated method stub

        bind(new ServicesTest()).to(ServicesTest.class);
//      bind(UserServiceImpl.class).to(IUserService.class).in(RequestScoped.class);

    }

}

服务:

IUserService 及其实现
public interface IUserService {

    public String method();
}


public class UserServiceImpl implements IUserService {

    @Inject
    public UserServiceImpl() {
        System.out.println("test");
    }

    @Override
    public String method() {
        return "Welcome ";
    }

}

其他
public class ServicesTest {

    public ServicesTest() {
        System.out.println("created ");
    }

    public String method() {
        return "Welcome";
    }
}

网页XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>com.walmart.learning.javaee</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>


</web-app>

现在,我可以使用访问我的资源
http://localhost:8080/javaeeLearning/rest/sayHello/h

这给了我以下错误
SEVERE: Servlet.service() for servlet [com.walmart.configuration.ResourceConfiguration] in context with path [/javaeeLearning] threw exception [A MultiException has 4 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=IUserService,parent=ControllerTest,qualifiers={},position=-1,optional=false,self=false,unqualified=null,2007960340)
2. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ServicesTest,parent=ControllerTest,qualifiers={},position=-1,optional=false,self=false,unqualified=null,10615079)
3. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.walmart.services.rest.controller.ControllerTest errors were found
4. java.lang.IllegalStateException: Unable to perform operation: resolve on com.walmart.services.rest.controller.ControllerTest
] with root cause

为了解决,我在资源配置中取消注释我的活页夹
然后我开始有 404。

请帮忙....

其他详情;

绒球
<name>javaeeLearning</name>
    <properties>
            <maven.compiler.source>10</maven.compiler.source>
            <maven.compiler.target>10</maven.compiler.target>
            <jaxrs.version>2.0.1</jaxrs.version>
            <jersey2.version>2.23</jersey2.version>
            <jersey2.gf.cdi.version>2.14</jersey2.gf.cdi.version>
        </properties>


<dependencies>
    <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>${jaxrs.version}</version>
    </dependency>



    <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.2</version>
    </dependency>

    <!-- Jersey2.x Dependencies -->

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey2.version}</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>${jersey2.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey2.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey2.version}</version>
    </dependency>



    <!-- Jersey2.x Dependency injection -->

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 -->
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.27</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-core -->
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0.1</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.4.0-b180830.0438</version>
    </dependency>


    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>




    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey2.version}</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers.glassfish/jersey-gf-cdi -->
    <dependency>
        <groupId>org.glassfish.jersey.containers.glassfish</groupId>
        <artifactId>jersey-gf-cdi</artifactId>
        <version>${jersey2.gf.cdi.version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.enterprise/cdi-api -->
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>2.0</version>
    </dependency>



</dependencies>

最佳答案

您可以在 beans.xml 中将 bean 发现模式设置为 all :

<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>

或 annatate UserServiceImpl带有范围注释(如 @Dependent )

关于jersey - 无法解决这个问题 UnsatisfiedDependencyException : There was no object available for injection at SystemInjecteeImpl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52496963/

相关文章:

java - 带有 gradle 的 Jersey 测试框架 - MessageBodyProviderNotFound

java - 将 Oauth 2.0 添加到基于 Jersey 的 RESTful 服务器

guice - 如何在 Jersey2 ServletContainer 中获取 HK2 ServiceLocator?

Java:方法链和依赖注入(inject)

java.lang.noclassdefounderror : org/glassfish/hk2/api/servicelocatorfactory$createpolicy 错误

java - Jersey 丢失 id 字段

java - 运行 Jersey sample

java - 返回 HTTP OK 响应的方法中哪种方法最好

java - HK2 与 Jersey 2 和 Apache Shiro 的依赖注入(inject)

java - 泽西 + Spring + hibernate