无法在 Tomcat 9 上 运行 Jersey 2 Restful Web 服务
Unable to run Jersey 2 Restful web service on Tomcat 9
我正在编写 Jersey 2 Restful 网络服务。
这是服务 class:
package com.Test.PS;
import java.io.IOException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.QueryParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import com.Test.Exchange.*; // Here class UserInfo is defined
@Path("/ps")
public class TestService {
private UserInfo ui;
public TestService () throws IOException {
ui = new UserInfo();
}
public TestService (String uid) throws IOException {
UserInfo ui = ObjectFileStore.serializeDataIn(uid);
}
public TestService (UserInfo ui) throws IOException {
this.ui = ui;
ObjectFileStore.serializeDataOut(ui);
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHelloHTML(@QueryParam("uid") String uid) {
String resource="<h1> Hi '" + uid + "'. </h1>";
return resource;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UserInfo postNK(@QueryParam("asid") String asid, UserInfo u) {
return ui;
}
}
这是 Maven 依赖项:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
最后,这是我的 web.xml 文件:
<display-name>Test-PS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Test-PS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.Test.PS</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test-PS</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
当 运行 此服务在 Tomcat 9 上使用 url (http://localhost:8081/Test-PS/rest/ps?uid=90) 时出现以下错误:
HTTP Status 404 – Not Found
Type Status Report
Message /Test-PS/rest/ps
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
在控制台屏幕上,我看到:
INFO: Reloading Context with name [/Test-PS] has started
org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/Test-PS] is completed
我尝试按照此处其他帖子的建议添加其他几个依赖项,但是,仍然有同样的问题。令人惊讶的是,有时它会出于未知原因起作用!!!
我也删除了所有的maven仓库,没有任何改变!!!
从所有 Jersey 依赖项中删除 <scope>provided</scope>
。仅当服务器的库中已有这些 jar 时才使用它。 Tomcat 没有 Jersey 罐子。另一方面,如果您使用的是 Glassfish,那么您可以使用 provided
,因为 Glassfish 已经拥有所有 Jersey 罐子。
删除 Jersey-client
和 jersey-server
依赖项。他们都已经被 jersey-container-servlet
拉进来了。不需要冗余。
问题解决方法:
- 正在删除 Eclipse 并安装 EE 版本。
- 正在删除 Tomcat 并安装 9.0.11 版本。
- 从头开始重新创建项目。
使用 jersey-container-servlet-core 而不是 jersey-container-servlet
参考:
泽西岛属地 - https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/modules-and-dependencies.html
Tomcat 版本比较 - http://tomcat.apache.org/whichversion.html
我正在编写 Jersey 2 Restful 网络服务。 这是服务 class:
package com.Test.PS;
import java.io.IOException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.QueryParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import com.Test.Exchange.*; // Here class UserInfo is defined
@Path("/ps")
public class TestService {
private UserInfo ui;
public TestService () throws IOException {
ui = new UserInfo();
}
public TestService (String uid) throws IOException {
UserInfo ui = ObjectFileStore.serializeDataIn(uid);
}
public TestService (UserInfo ui) throws IOException {
this.ui = ui;
ObjectFileStore.serializeDataOut(ui);
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHelloHTML(@QueryParam("uid") String uid) {
String resource="<h1> Hi '" + uid + "'. </h1>";
return resource;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public UserInfo postNK(@QueryParam("asid") String asid, UserInfo u) {
return ui;
}
}
这是 Maven 依赖项:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
<scope>provided</scope>
</dependency>
最后,这是我的 web.xml 文件:
<display-name>Test-PS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Test-PS</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.Test.PS</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test-PS</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
当 运行 此服务在 Tomcat 9 上使用 url (http://localhost:8081/Test-PS/rest/ps?uid=90) 时出现以下错误:
HTTP Status 404 – Not Found
Type Status Report
Message /Test-PS/rest/ps
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
在控制台屏幕上,我看到:
INFO: Reloading Context with name [/Test-PS] has started
org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/Test-PS] is completed
我尝试按照此处其他帖子的建议添加其他几个依赖项,但是,仍然有同样的问题。令人惊讶的是,有时它会出于未知原因起作用!!!
我也删除了所有的maven仓库,没有任何改变!!!
从所有 Jersey 依赖项中删除
<scope>provided</scope>
。仅当服务器的库中已有这些 jar 时才使用它。 Tomcat 没有 Jersey 罐子。另一方面,如果您使用的是 Glassfish,那么您可以使用provided
,因为 Glassfish 已经拥有所有 Jersey 罐子。删除
Jersey-client
和jersey-server
依赖项。他们都已经被jersey-container-servlet
拉进来了。不需要冗余。
问题解决方法:
- 正在删除 Eclipse 并安装 EE 版本。
- 正在删除 Tomcat 并安装 9.0.11 版本。
- 从头开始重新创建项目。
使用 jersey-container-servlet-core 而不是 jersey-container-servlet
参考:
泽西岛属地 - https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/modules-and-dependencies.html
Tomcat 版本比较 - http://tomcat.apache.org/whichversion.html