在Tomcat 8上实施Jersey 2.x的最佳方法是什么?

rss*_*919 13 java jersey-2.0 tomcat8

我有Web容器和Tomcat的知识,可以部署静态和动态网站.但我是REST和泽西新手.我已阅读2.6用户指南,查看了许多网站和YouTube视频.在1.x Jersey上似乎有很多信息,但在2.x上没有太多信息我可以在我的环境中使用1.18,但似乎无法使任何部署模型适用于2.x. 我在2.x中注意到有一个应用程序部署模型.所以我想我会问一些非常通用的问题来开始.

  1. 哪种部署模型最适合通过Tomcat 8进行基本REST服务?为什么?
  2. 我看到使用2.6部署的.jars与使用1.18部署的.jars有很大不同.有没有一种简单的方法可以告诉您基本的Tomcat安装需要哪些罐子?
  3. 如果你有一个基本的例子,那就太好了.

谢谢

6ce*_*cef 40

以下是我希望相对完整的解决方案.

你没有提到Maven,所以我会:Maven是你的朋友.

让我们从一个pom开始:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example.groupid</groupId>
  <artifactId>stack</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet-core</artifactId>
      <version>2.13</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.13</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>stack</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.5</version>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <name>Stack</name>
</project>
Run Code Online (Sandbox Code Playgroud)

就依赖性而言,这可能不是绝对最小值,但它很接近.

但那只是pom.技巧继续在web.xml和Java类中.

关于那个web.xml ......

这太疯狂了,所以请耐心等待:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         metadata-complete="false"
         version="3.1">
</web-app>
Run Code Online (Sandbox Code Playgroud)

好吧,也许不那么复杂.

请注意,设置metadata-complete="true"可能会导致Tomcat启动更快.

一对Java类

一个是"申请",另一个是休息.***

其余的电话非常简单:

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!";
  }
}
Run Code Online (Sandbox Code Playgroud)

该应用程序如下所示:

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<Class<?>> getClasses() {
    return new HashSet<Class<?>>(Arrays.asList(HelloRest.class));
  }
}
Run Code Online (Sandbox Code Playgroud)

就是这样.当您导航到类似的东西时http://localhost:8080/stack/rest/hello,您应该看到文本"Hello,rest!"

稍微利用泽西岛.

getClasses()在RestApp中有点难看.您可以使用Jersey的ResourceConfig,如Jersey用户指南,它看起来像这样:

public class RestApp extends ResourceConfig {
    public RestApp() {
        packages("some.package");
    }
}
Run Code Online (Sandbox Code Playgroud)

但我不想使用Maven!

精细.这些是Eclipse列为Maven依赖项的jar:

  • 的javax.servlet-API-3.1.0.jar
  • 新泽西州容器servlet的核心2.13.jar
  • javax.inject-2.3.0-b10.jar
  • 新泽西州共2.13.jar
  • javax.annotation中,API-1.2.jar
  • 新泽西州番石榴2.13.jar
  • HK2-API-2.3.0-b10.jar
  • HK2-utils的-2.3.0-b10.jar
  • aopalliance-重新包装-2.3.0-b10.jar
  • HK2定位器-2.3.0-b10.jar
  • Javassist进行-3.18.1-GA.jar
  • OSGi的资源定位器1.0.1.jar
  • 新泽西服务器2.13.jar
  • 新泽西州的客户 - 2.13.jar
  • 验证-API 1.1.0.Final.jar
  • javax.ws.rs-API-2.0.1.jar
  • 新泽西州容器servlet的2.13.jar

据推测,手动将这些添加到类路径应该可行.或者使用Maven.

  • 哇!这是一个极好的答案!不要以为我曾经遇到过更完整的答案...... (3认同)

rss*_*919 1

我能够使用 Jersey 2.6 用户指南中提供的用于部署到 3.x servlet 容器的说明来完成这项工作。我最终使用了与下面的项目类似的东西。由于 URL 映射是在 .xml 中提供的,因此您可以从 Application 子类中省略 @ApplicationPath。

<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <!-- Servlet declaration can be omitted in which case
         it would be automatically added by Jersey -->
    <servlet>
        <servlet-name>org.example.MyApplication</servlet-name>
    </servlet>

    <!-- Servlet mapping can be omitted in case the Application subclass
         is annotated with @ApplicationPath annotation; in such case
         the mapping would be automatically added by Jersey -->
    <servlet-mapping>
        <servlet-name>org.example.MyApplication</servlet-name>
        <url-pattern>/myresources/*</url-pattern>
    </servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)