我正在编写Jersey 2 Restful Web服务。 这是服务类:
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>
在使用URL(http://localhost:8081/Test-PS/rest/ps?uid=90)在Tomcat 9上运行此服务时,出现以下错误:
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存储库,没有任何更改!
答案 0 :(得分:0)
从所有Jersey依赖项中删除<scope>provided</scope>
。仅当服务器库中已包含这些jar时才使用此功能。 Tomcat没有球衣罐。另一方面,如果您使用的是Glassfish,则可以使用provided
,因为Glassfish已经拥有所有的Jersey罐子。
删除Jersey-client
和jersey-server
依赖性。他们俩都已被jersey-container-servlet
吸引。不需要冗余。
答案 1 :(得分:0)
问题的解决方法是:
答案 2 :(得分:0)
使用 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