Basic full configuration for Jersey on Tomcat in eclipse

后端 未结 2 1388
独厮守ぢ
独厮守ぢ 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:53

    Adding to the answer, if you are using jackson then dependency is already mentioned as jersey-media-json-jackson -

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    

    There are other json providers as well.

    If you want to use MOXy as your JAXB implementation, then you can use

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    

    To use JSON-P as your JSON provider you need to add

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-processing</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    

    To use Jettison as your JSON provider you need to add jersey-media-json-jettison module to your pom.xml file:

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jettison</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    

    To use multipart features you need to add jersey-media-multipart module to your pom.xml file:

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    

    Further details, please refer to https://jersey.java.net/documentation/latest/media.html

    0 讨论(0)
  • 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

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>jersey2</groupId>
      <artifactId>jersey2</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jersey.version>2.23.1</jersey.version>
    </properties>
    
    <dependencies>
    
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    
        <dependency>
           <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    
    </dependencies>
    

    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;
      }
    }
    
    0 讨论(0)
提交回复
热议问题