Basic full configuration for Jersey on Tomcat in eclipse

99封情书 提交于 2019-12-31 04:50:08

问题


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 omit a part of the process (e.g. with Spring, with a web.xml file, deploying a non-Jersey application on Tomcat, etc.).

Here's where I am so far:

I created a Dynamic Web Project called TestProject in eclipse, and converted it to a maven project.

Here's my 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>test</groupId>
<artifactId>test</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.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
        <scope>provided</scope>
    </dependency>

    <!-- POJO to JSON support -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <!-- required to run without web.xml on Tomcat -->
    <dependency>
        <groupId>org.glassfish.jersey.bundles</groupId>
        <artifactId>jaxrs-ri</artifactId>
        <version>${jersey.version}</version>
    </dependency>

</dependencies>

I have a package com.test which contains 3 classes:

TestApplication to create an application without a web.xml:

@ApplicationPath("/*")
public class TestApplication extends ResourceConfig {

    public TestApplication(){
        packages("com.test");
        register(JacksonFeature.class);
    }

}

TestProvider as a minimal resource class:

@Path("/test")
public class TestProvider {

    @GET
    @Produces("application/json")
    public Test getTest(){
        return new Test();
    }

}

Test as a minimal object to return in JSON format:

public class Test {

    private String field = "test";

}

I have a Tomcat server running with the project added as a web module. If I run the project (Run as > Run on server), and navigate to http://localhost:8080/test or http://localhost:8080/TestProject/test I get a standard tomcat 404.

Here's my Tomcat startup log:

jun 14, 2016 11:29:06 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TestProject' did not find a matching property.
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.35
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          May 11 2016 21:57:08 UTC
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.35.0
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 7
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.1
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jre1.8.0_71
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_71-b15
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\tim\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Users\tim\workspace\apache-tomcat-8.0.35
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\tim\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Users\tim\workspace\apache-tomcat-8.0.35
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\tim\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\Users\tim\workspace\apache-tomcat-8.0.35\endorsed
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
jun 14, 2016 11:29:06 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_71\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_71/bin/server;C:/Program Files/Java/jre1.8.0_71/bin;C:/Program Files/Java/jre1.8.0_71/lib/amd64;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\ImageMagick-6.9.1-Q16;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\ProgramFiles\apache-maven-3.2.5\bin;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\eclipse;;.
jun 14, 2016 11:29:06 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
jun 14, 2016 11:29:06 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
jun 14, 2016 11:29:06 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
jun 14, 2016 11:29:06 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
jun 14, 2016 11:29:06 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 941 ms
jun 14, 2016 11:29:06 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
jun 14, 2016 11:29:06 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.35
jun 14, 2016 11:29:07 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [187] milliseconds.
jun 14, 2016 11:29:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
jun 14, 2016 11:29:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
jun 14, 2016 11:29:07 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 668 ms

If I create a non-jersey HttpServlet with the annotation @WebServlet(name="hello",urlPatterns={"/hello"}), I do get a response at http://localhost:8080/TestProject/hello.

What am I missing? Also, is any of the above not 'best practice' for a basic web application? It seems this technology changes quite rapidly, and it's difficult to know which examples or methods are outdated.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/37778893/basic-full-configuration-for-jersey-on-tomcat-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!