4

我已经尝试了很多来解决这个问题,但不能。我发现,如果我们使用“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

为了解决这个问题,我在资源配置中取消注释我的 Binder,然后我开始拥有 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>
4

2 回答 2

2

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

<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

于 2018-09-26T11:12:33.583 回答
1

使您的 JAX-RS 资源成为 CDI bean,如下所示:

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

然后你不需要活页夹,注入应该可以工作。

于 2018-09-25T20:57:04.363 回答