[英]JAX-RS web service is working on GlassFish Server 4.0 but not working on Tomcat
I am new to web services. 我是网络服务的新手。 So I started with a small program like the following.
所以我开始使用如下的小程序。
It's working perfectly in GlassFish server but not in Tomcat (I want to run on Tomcat). 它在GlassFish服务器中运行完美,但在Tomcat中运行不正常(我想在Tomcat上运行)。 It's a simple program which gives only the idea of how to run the web service application.
这是一个简单的程序,它只给出了如何运行Web服务应用程序的想法。
FirstRestService.java: FirstRestService.java:
package com.sandy.demo;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/resources")
public class FirstRestService extends Application {
}
Employees.java: Employees.java:
package com.sandy.demo;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/employees")
public class Employees {
@GET
public String getEmployeesNames() {
return "Hello World";
}
}
I have included the jsr311-api-1.1.1.jar
(JAX-RS API JAR file). 我已经包含了
jsr311-api-1.1.1.jar
(JAX-RS API JAR文件)。
I took a working WAR file of the application and deployed in GlassFish Server. 我获取了应用程序的工作WAR文件并部署在GlassFish Server中。 Then I ran the server with the URL:
http://localhost:8080/MyFirstRestApplication/resources/employees
. 然后我使用URL运行服务器:
http://localhost:8080/MyFirstRestApplication/resources/employees
。
But I am unable to do the same in Tomcat. 但我无法在Tomcat中做同样的事情。
Please note that jsr311-api-1.1.1.jar
refers to JAX-RS 1.1 . 请注意,
jsr311-api-1.1.1.jar
是指JAX-RS 1.1 。 GlassFish Server 4.0 uses JAX-RS 2.0 . GlassFish Server 4.0使用JAX-RS 2.0 。
Besides the correct JAX-RS dependency JAR, you'll need a JAX-RS implementation such as Jersey (used by GlassFish ) or RESTEasy (used by WildFly ). 除了正确的JAX-RS依赖JAR之外,您还需要一个JAX-RS实现,例如Jersey (由GlassFish使用 )或RESTEasy (由WildFly使用 )。
GlassFish already contains all JAX-RS 2.0 and Jersey dependencies you need. GlassFish已包含您需要的所有JAX-RS 2.0和Jersey依赖项。 Tomcat doesn't.
Tomcat没有。
Since you were using GlassFish, probably you'll choose Jersey. 由于您使用的是GlassFish,可能您会选择Jersey。 There are two ways to include JAX-RS and Jersey in your Tomcat project:
在Tomcat项目中有两种方法可以包含JAX-RS和Jersey:
If you are using Maven, add the following dependencies to your pom.xml
: 如果您使用的是Maven,请将以下依赖项添加到您的
pom.xml
:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0,
use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
<!-- Required only when you are using JAX-RS Client -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.22.1</version>
</dependency>
Just ensure you are using the correct artifactId
according to your Tomcat Servlet API version: if your Tomcat implements Servlet API older than 3.0, use jersey-container-servlet-core
. 根据您的Tomcat Servlet API版本,确保使用正确的
artifactId
:如果您的Tomcat实现了早于3.0的Servlet API,请使用jersey-container-servlet-core
。 Otherwise, use jersey-container-servlet
. 否则,请使用
jersey-container-servlet
。
On Tomcat documentation you'll find information about the Servlet API version used by each Tomcat version. 在Tomcat文档中,您将找到有关每个Tomcat版本使用的Servlet API版本的信息。 To summarize:
总结一下:
The latest version of each artifactId
can be checked in the Maven Repository: 可以在Maven资源库中检查每个
artifactId
的最新版本:
See more information about Jersey dependencies on the documentation page . 在文档页面上查看有关Jersey依赖项的更多信息。
If you are not using Maven, refer to Jersey download page , download the Jersey JAX-RS 2.0 RI bundle and include the dependencies on the classpath. 如果您没有使用Maven,请参阅Jersey下载页面 ,下载Jersey JAX-RS 2.0 RI包并在类路径中包含依赖项。
The Jersey JAX-RS 2.0 RI bundle contains the JAX-RS 2.0 API JAR, all the core Jersey module JARs as well as all the required 3rd-party dependencies. Jersey JAX-RS 2.0 RI包包含JAX-RS 2.0 API JAR,所有核心Jersey模块JAR以及所有必需的第三方依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.