在Tomcat 8上实现Jersey 2.x的最佳方法是什么? [英] What is best approach for implementing Jersey 2.x on Tomcat 8?

查看:77
本文介绍了在Tomcat 8上实现Jersey 2.x的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Web容器和Tomcat有所了解,并且可以部署静态和动态Web站点.但是我是REST和Jersey的新手.我已阅读2.6用户指南,回顾了许多网站和youtube视频.在1.x Jersey上似乎有很多信息,但在2.x上却没有太多信息,我可以在环境中使用1.18,但似乎无法在2.x上使用任何部署模型.我注意到在2.x中有一个应用程序部署模型.所以我想我会问一些非常通用的问题来开始这一工作.

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.

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

谢谢

推荐答案

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

What follows are what I hope are relatively complete solutions to your questions.

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

You don't mention Maven, so I will: Maven is your friend here.

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

但这只是pom.棘手的事情在web.xml和Java类中继续.

But that's just the pom. The trickery continues in the web.xml and the Java classes.

这太疯狂了,所以请忍受:

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.

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

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

一个是应用程序",另一个是其余的调用.***

One is the "application", the other is the rest call.***

其余调用非常简单:

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

该应用程序如下所示:

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

就是这样.导航到http://localhost:8080/stack/rest/hello之类的内容时,应该看到文本你好,休息!"

And that's it. When you navigate to something like http://localhost:8080/stack/rest/hello, you should see the text "Hello, rest!"

getClasses()有点难看.您可以使用《 Jersey用户指南》 中的Jersey的ResourceConfig. :

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:

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

但是我不想使用Maven!

好.这些是Eclipse列出的jar作为Maven依赖项:

But I don't want to use Maven!

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

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

大概可以手动将其添加到您的类路径中.或使用Maven.

Presumably, adding those manually to your classpath should work. Or use Maven.

这篇关于在Tomcat 8上实现Jersey 2.x的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆