embedded jetty:依赖注入未发生

mrfwxfqh  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(228)

很抱歉发了这么长的邮件。不知道什么时候太多了。问题是依赖注入没有在jersey rest servlet上发生。根据产品设计师的要求使用嵌入式码头和休息区。以下是我的一些代码:
应用服务器.java

  1. Configuration.ClassList classList = Configuration.ClassList.setServerDefault(server);
  2. classList.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
  3. "org.eclipse.jetty.plus.webapp.EnvConfiguration",
  4. "org.eclipse.jetty.plus.webapp.PlusConfiguration");
  5. classList.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
  6. "org.eclipse.jetty.annotations.AnnotationConfiguration");
  7. ServletContextHandler servletCtx = new ServletContextHandler(ServletContextHandler.SESSIONS);
  8. servletCtx.setContextPath("/rest/v1");
  9. ServletHolder jerseyServlet = servletCtx.addServlet(
  10. org.glassfish.jersey.servlet.ServletContainer.class
  11. , "/*");
  12. jerseyServlet.setInitOrder(0);
  13. jerseyServlet.setInitParameter(
  14. "jersey.config.server.provider.classnames",
  15. MbiResource.class.getCanonicalName());
  16. WebAppContext webAppCtx = new WebAppContext();
  17. webAppCtx.setResourceBase("webapp");
  18. webAppCtx.setContextPath("/");
  19. HandlerList handlers = new HandlerList();
  20. handlers.setHandlers(new Handler[]{servletCtx,webAppCtx,new DefaultHandler()});
  21. server.setHandler(handlers);

重新资源.java

  1. import org.apache.tomcat.jdbc.pool.DataSource;
  2. import javax.annotation.Resource;
  3. import javax.json.JsonObject;
  4. import javax.ws.rs.Consumes;
  5. import javax.ws.rs.GET;
  6. import javax.ws.rs.Path;
  7. import javax.ws.rs.Produces;
  8. import javax.ws.rs.core.MediaType;
  9. import javax.ws.rs.core.Response;
  10. @Path("mbi")
  11. @Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
  12. @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
  13. public class MbiResource {
  14. **//this does not work**
  15. @Resource(mappedName = "jdbc/warehouse-datasource")
  16. DataSource dataSource;
  17. @GET
  18. public String get(){
  19. **//this does work**
  20. try{
  21. InitialContext ic = new InitialContext();
  22. dataSource = (DataSource) ic.lookup("jdbc/warehouse-datasource");
  23. return dataSource.toString();
  24. }catch (Exception ex){
  25. return ex.getMessage();
  26. }
  27. }
  28. }

pom(所有依赖的原因是强制相同的版本)

  1. <jetty.version>9.4.35.v20201120</jetty.version>
  2. <dependency>
  3. <groupId>org.eclipse.jetty</groupId>
  4. <artifactId>jetty-annotations</artifactId>
  5. <version>${jetty.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.eclipse.jetty</groupId>
  9. <artifactId>jetty-http</artifactId>
  10. <version>${jetty.version}</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.eclipse.jetty</groupId>
  14. <artifactId>jetty-jndi</artifactId>
  15. <version>${jetty.version}</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.eclipse.jetty</groupId>
  19. <artifactId>jetty-jsp</artifactId>
  20. <version>9.2.30.v20200428</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.eclipse.jetty</groupId>
  24. <artifactId>jetty-plus</artifactId>
  25. <version>${jetty.version}</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.eclipse.jetty</groupId>
  29. <artifactId>jetty-server</artifactId>
  30. <version>${jetty.version}</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.eclipse.jetty</groupId>
  34. <artifactId>jetty-servlet</artifactId>
  35. <version>${jetty.version}</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.eclipse.jetty</groupId>
  39. <artifactId>jetty-webapp</artifactId>
  40. <version>${jetty.version}</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.ow2.asm</groupId>
  44. <artifactId>asm</artifactId>
  45. <version>9.0</version>
  46. </dependency>

这些是我用来研究这个问题的一些参考资料。
https://www.eclipse.org/jetty/documentation/jetty-9/index.html#advanced-嵌入
如何将jetty和jersey嵌入到java应用程序中
嵌入式jetty找不到带注解的servlet
jersey jdbc@resource不工作

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题