无法解决此问题UnsatisfiedDependencyException:SystemInjecteeImpl

时间:2018-09-25 11:20:02

标签: 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";
    }
}

WEbXML

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

请帮助。...

其他详细信息;

Pom

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

2 个答案:

答案 0 :(得分:1)

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

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

或使用范围注释(例如UserServiceImpl)对@Dependent进行命名

答案 1 :(得分:0)

使您的JAX-RS资源像这样的CDI bean:

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

然后您就不需要粘合剂,注射应该可以了。