gpt4 book ai didi

java - Jersey HK2 依赖性错误

转载 作者:行者123 更新时间:2023-11-30 02:26:14 29 4
gpt4 key购买 nike

我正在使用 Jersey 创建 RESTful Web 服务。我还使用 Jetty 嵌入式 Web 服务器,它通过 Java main 方法运行。 Jersey 默认带有 HK2 DI。我遇到了服务依赖注入(inject)的问题:

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 中回答了问题,但似乎我的工作在依赖注入(inject)方面仍然存在问题。

我有以下代码:

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());

关于java - Jersey HK2 依赖性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45586315/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com