What is best approach for implementing Jersey 2.x on Tomcat 8?

后端 未结 2 842
花落未央
花落未央 2021-01-31 00:27

I have Knowledge of the Web container and Tomcat and can deploy static and dynamic web sites. But I am new to REST and Jersey. I have read the 2.6 user\'s guide, reviewed many

2条回答
  •  甜味超标
    2021-01-31 01:09

    What follows are what I hope are relatively complete solutions to your questions.

    You don't mention Maven, so I will: Maven is your friend here.

    Let's start with a pom:

    
      4.0.0
      com.example.groupid
      stack
      war
      0.0.1-SNAPSHOT
      http://maven.apache.org
      
        
          javax.servlet
          javax.servlet-api
          3.1.0
          provided
        
        
          org.glassfish.jersey.containers
          jersey-container-servlet-core
          2.13
        
        
          org.glassfish.jersey.containers
          jersey-container-servlet
          2.13
        
      
      
        stack
        
          
            org.apache.maven.plugins
            maven-war-plugin
            2.5
          
    
          
            org.apache.maven.plugins
            maven-compiler-plugin
            3.2
            
              1.8
              1.8
            
          
        
      
      Stack
    
    

    That might not be the absolute minimum in terms of dependencies, but it's close.

    But that's just the pom. The trickery continues in the web.xml and the Java classes.

    About that web.xml...

    It's insanely complicated, so bear with me:

    
    
    

    Ok, maybe not that complicated.

    Note that setting metadata-complete="true" may result in Tomcat starting faster.

    A pair of Java classes

    One is the "application", the other is the rest call.***

    The rest call is pretty straightforward:

    package some.package;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    
    @Path("/hello")
    public class HelloRest {
    
      @GET
      public String message() {
        return "Hello, rest!";
      }
    }
    

    The application looks like this:

    package some.package;
    
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    import some.package.HelloRest;
    
    @ApplicationPath("/rest")
    public class RestApp extends Application {
      public Set> getClasses() {
        return new HashSet>(Arrays.asList(HelloRest.class));
      }
    }
    

    And that's it. When you navigate to something like http://localhost:8080/stack/rest/hello, you should see the text "Hello, rest!"

    Leverage Jersey a bit.

    getClasses() in RestApp is a little ugly. You might use Jersey's ResourceConfig, as at the Jersey User's Guide, which would look like this:

    public class RestApp extends ResourceConfig {
        public RestApp() {
            packages("some.package");
        }
    }
    

    But I don't want to use Maven!

    Fine. These are the jars Eclipse lists as Maven dependencies:

    • javax.servlet-api-3.1.0.jar
    • jersey-container-servlet-core-2.13.jar
    • javax.inject-2.3.0-b10.jar
    • jersey-common-2.13.jar
    • javax.annotation-api-1.2.jar
    • jersey-guava-2.13.jar
    • hk2-api-2.3.0-b10.jar
    • hk2-utils-2.3.0-b10.jar
    • aopalliance-repackaged-2.3.0-b10.jar
    • hk2-locator-2.3.0-b10.jar
    • javassist-3.18.1-GA.jar
    • osgi-resource-locator-1.0.1.jar
    • jersey-server-2.13.jar
    • jersey-client-2.13.jar
    • validation-api-1.1.0.Final.jar
    • javax.ws.rs-api-2.0.1.jar
    • jersey-container-servlet-2.13.jar

    Presumably, adding those manually to your classpath should work. Or use Maven.

提交回复
热议问题