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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22083164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:29:26  来源:igfitidea点击:

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

javajersey-2.0tomcat8

提问by rss181919

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 sites and youtube videos. There seems to be a lot of info on 1.x Jersey but not much on 2.x I can get 1.18 working in my environment but can't seem to get any deployment models working for 2.x. I noticed in 2.x there is an Application deployment model. So I thought i would ask some very generic questions to get this started.

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

  1. Which deployment model is best for basic REST services through Tomcat 8 and why?
  2. I see that the .jars deployed with 2.6 are much different than the ones deployed with 1.18. Is there an easy way to tell which jars you need for a basic Tomcat installation?
  3. If you have a basic example, that would be great.
  1. 哪种部署模型最适合通过 Tomcat 8 实现的基本 REST 服务,为什么?
  2. 我看到使用 2.6 部署的 .jar 与使用 1.18 部署的有很大不同。有没有一种简单的方法来判断基本的 Tomcat 安装需要哪些 jars?
  3. 如果你有一个基本的例子,那就太好了。

Thanks

谢谢

回答by rss181919

I was able to get this working using the directions supplied in the Jersey 2.6 user's guide for deployment to a 3.x servlet container. I ended up using something similar to the item below. Because the URL mapping is supplied in the .xml, you can omit @ApplicationPath from the Application subclass.

我能够使用 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>

回答by 6cef

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.

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

Let's start with a pom:

让我们从一个 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>

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.

但这只是 pom。诡计在 web.xml 和 Java 类中继续存在。

About that web.xml...

关于那个 web.xml ...

It's insanely complicated, so bear with me:

它非常复杂,所以请耐心等待:

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

Ok, maybe not that complicated.

好吧,也许没那么复杂。

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

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

A pair of Java classes

一对 Java 类

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<Class<?>> getClasses() {
    return new HashSet<Class<?>>(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!"

就是这样。当您导航到类似的内容时http://localhost:8080/stack/rest/hello,您应该会看到文本“您好,休息!”

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:

getClasses()在 RestApp 中有点难看。您可以使用 Jersey 的 ResourceConfig,如Jersey User's Guide 中所示,如下所示:

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

But I don't want to use Maven!

但我不想使用Maven!

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

美好的。这些是 Eclipse 列为 Maven 依赖项的 jars:

  • 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
  • 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
  • 球衣番石榴-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
  • 验证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.

据推测,将这些手动添加到您的类路径应该可以工作。或者使用 Maven。