很抱歉发了这么长的邮件。不知道什么时候太多了。问题是依赖注入没有在jersey rest servlet上发生。根据产品设计师的要求使用嵌入式码头和休息区。以下是我的一些代码:
应用服务器.java
Configuration.ClassList classList = Configuration.ClassList.setServerDefault(server);
classList.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
"org.eclipse.jetty.plus.webapp.EnvConfiguration",
"org.eclipse.jetty.plus.webapp.PlusConfiguration");
classList.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
"org.eclipse.jetty.annotations.AnnotationConfiguration");
ServletContextHandler servletCtx = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletCtx.setContextPath("/rest/v1");
ServletHolder jerseyServlet = servletCtx.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class
, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter(
"jersey.config.server.provider.classnames",
MbiResource.class.getCanonicalName());
WebAppContext webAppCtx = new WebAppContext();
webAppCtx.setResourceBase("webapp");
webAppCtx.setContextPath("/");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{servletCtx,webAppCtx,new DefaultHandler()});
server.setHandler(handlers);
重新资源.java
import org.apache.tomcat.jdbc.pool.DataSource;
import javax.annotation.Resource;
import javax.json.JsonObject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("mbi")
@Consumes({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
@Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
public class MbiResource {
**//this does not work**
@Resource(mappedName = "jdbc/warehouse-datasource")
DataSource dataSource;
@GET
public String get(){
**//this does work**
try{
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup("jdbc/warehouse-datasource");
return dataSource.toString();
}catch (Exception ex){
return ex.getMessage();
}
}
}
pom(所有依赖的原因是强制相同的版本)
<jetty.version>9.4.35.v20201120</jetty.version>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jndi</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.2.30.v20200428</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-plus</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.0</version>
</dependency>
这些是我用来研究这个问题的一些参考资料。
https://www.eclipse.org/jetty/documentation/jetty-9/index.html#advanced-嵌入
如何将jetty和jersey嵌入到java应用程序中
嵌入式jetty找不到带注解的servlet
jersey jdbc@resource不工作
暂无答案!
目前还没有任何答案,快来回答吧!