Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey HK2依赖项错误_Java_Dependency Injection_Jersey_Jetty_Hk2 - Fatal编程技术网

Java Jersey HK2依赖项错误

Java Jersey HK2依赖项错误,java,dependency-injection,jersey,jetty,hk2,Java,Dependency Injection,Jersey,Jetty,Hk2,我正在使用Jersey创建RESTful web服务。我还使用Jetty嵌入式web服务器,它通过Java main方法运行。默认情况下,球衣附带HK2 DI。我对服务的依赖项注入有疑问: Aug 09, 2017 4:16:30 PM org.glassfish.jersey.internal.Errors logErrors WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure det

我正在使用Jersey创建RESTful web服务。我还使用Jetty嵌入式web服务器,它通过Java main方法运行。默认情况下,球衣附带HK2 DI。我对服务的依赖项注入有疑问:

Aug 09, 2017 4:16:30 PM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=HelloService,parent=HelloResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,18651401)
在Stackoverflow中,我已经看过一些教程并回答了一些问题,但我的工作似乎仍然存在依赖注入方面的问题

我有以下代码:

package hello.config;
...    
public class ApplicationResourceConfig extends ResourceConfig {
    public ApplicationResourceConfig() {
            packages("hello.resource");
            register(ApplicationBinder.class);
            register(JacksonFeature.class);
        }
}

package hello.config;
...
public class ApplicationBinder extends AbstractBinder {
    @Override
        protected void configure() {
        bind(HelloServiceImpl.class).to(HelloService.class).in(RequestScoped.class);
            bind(HelloDaoImpl.class).to(HelloDao.class).in(RequestScoped.class);
    }
}

package hello.resource;
...
@Path("/hello")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class HelloResource {
        @Inject
        private HelloService helloService;

        @GET
        @Path("world")
        public String world() {
            return helloService.hello();
    }
}

package hello.service;
...
@Contract
public interface HelloService {
    void hello();
}

package hello.service;
...
@Service
@Named
public class HelloServiceImpl implements HelloService {
    @Inject
    private HelloDao helloDao;

    @Override
    public String hello(hello) {
        helloDao.hello();
    }
}

package hello.dao;
...
@Contract
public interface HelloDao {
    String hello();
}

package hello.dao;
...
@Named
public interface HelloDaoImpl {
    public String hello() {
        return "Hello, world!";
    }
}

package hello;
...
public class Server {
    private static final int DEFAULT_PORT = 8080;
    private static final String DEFAULT_CONTEXT_PATH = "/myapp";
    private static final String DEFAULT_MAPPING_URL = "/*";

    public static void main(String[] args) {
        Server server = new Server(port(args));
        ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContextHandler.setContextPath(DEFAULT_CONTEXT_PATH);
        servletContextHandler.addServlet(new ServletContainer(new ApplicationResourceConfig()), DEFAULT_MAPPING_URL);
        server.setHandler(servletContextHandler);
        server.start();
        LOGGER.info("Server started at port {}", port);
        server.join();
    }

    public static int port(String[] args) {
        if (args.length > 0) {
        String port = args[0];
        try {
            return Integer.valueOf(port);
        } catch (NumberFormatException exception) {
            LOGGER.error("Invalid port number {}", port);
        }

        return DEFAULT_PORT;
    }
}

谢谢。

您不能将
ApplicationBinder
注册为类。它需要注册为一个实例

register(new ApplicationBinder());