2

Apologies for the long post. Do not know when is too much. The issue is that dependency injection is not occurring on Jersey REST servlet. Using Embedded Jetty and REST as required by the product designer. Here is some of the code I have:

AppServer.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);

RestResource.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 (The reason for all dependencies is to force the same version)

<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>

These are some of the references I used to research this issue.

https://www.eclipse.org/jetty/documentation/jetty-9/index.html#advanced-embedding

How to embed Jetty and Jersey into my Java application

Embedded Jetty does not find Annotated Servlet

Jersey jdbc @resource not working

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Tim Murphy
  • 21
  • 2
  • You need to either try to enable [CDI support](https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/cdi.support.html#cdi.support.existing.containers) or use HK2 to create a custom injection. – Paul Samsotha Dec 19 '20 at 02:38
  • Thank you for the quick response. I will try that. It would help if I asked the right question. – Tim Murphy Dec 20 '20 at 05:43

0 Answers0