Basic full configuration for Jersey on Tomcat in eclipse

后端 未结 2 1408
独厮守ぢ
独厮守ぢ 2021-01-24 02:23

I\'m new to Jersey and trying to set up a basic webapp using Tomcat and eclipse. I\'ve looked at numerous tutorials and examples, but they\'re all different from each other, or

2条回答
  •  攒了一身酷
    2021-01-24 02:57

    Jersey 2 Tomcat 8, Java 8 & Servlet 3.0 Basic Minimal Configuration

    http://localhost:{port} to access the tomcat manager. You can log in to find your context path.

    Acces your application at:

    http://localhost:{port}/{context-path}/{url-pattern}/{resource-path}

    pom.xml

    
      4.0.0
      jersey2
      jersey2
      0.0.1-SNAPSHOT
      war
      
        src
        
          
            maven-compiler-plugin
            3.3
            
              1.8
              1.8
            
          
          
            maven-war-plugin
            2.6
            
              WebContent
              false
            
          
        
      
    
    
        UTF-8
        2.23.1
    
    
    
    
        
            org.glassfish.jersey.media
            jersey-media-json-jackson
            ${jersey.version}
        
    
        
            org.glassfish.jersey.core
            jersey-server
            ${jersey.version}
        
    
        
           org.glassfish.jersey.containers
            jersey-container-servlet
            ${jersey.version}
        
    
    
    

    Application

    package com.test;
    
    import javax.ws.rs.ApplicationPath;
    import org.glassfish.jersey.jackson.JacksonFeature;
    import org.glassfish.jersey.server.ResourceConfig;
    
    @ApplicationPath("/*")
    public class TestApplication extends ResourceConfig {
    
        public TestApplication(){
            packages("com.test");
            register(JacksonFeature.class);
        }
    
    }
    

    Resource

    package com.test;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    
    @Path("/test")
    public class TestResource {
    
        @GET
        @Produces("application/json")
        public Test getTest(){
            Test bean = new Test();
            bean.setName("Jersey");
            bean.setVersion("2.0");
            bean.setServlet("3.0");
            return bean;
        }
    
    }
    

    Test Bean

    package com.test;
    
    public class Test {
    
      private String name;
      private String version;
      private String servlet;
    
      public String getName() {
        return name;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public String getVersion() {
        return version;
      }
    
      public void setVersion(String version) {
        this.version = version;
      }
    
      public String getServlet() {
        return servlet;
      }
    
      public void setServlet(String servlet) {
        this.servlet = servlet;
      }
    }
    

提交回复
热议问题