这让我疯了。我已经尝试了大约五个关于同一错误的Stack Overflow问题,但无济于事。
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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"
version="3.1">
<display-name>Spotitube</display-name>
<servlet>
<servlet-name>Jersey Web Application</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.spotitube</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我的服务:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/myresource")
public class myresource {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@GET
@Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN })
public String getIt() {
return "Got it!";
}
}
我的pom.xml,它应该以某种方式相关:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spotitube</groupId>
<artifactId>spotitube3</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.23.2</jersey.version>
</properties>
</project>
index.jsp只包含一个指向localhost:8080 / rest / myresource服务的链接。 URL签出。
我尝试过Tomcat 7和8.5。我的IDE是IntelliJ 2017.2.5。我已经按照我所遵循的教程添加了所有jar文件,并且Tomcat工作正常。只有在RESTful页面(而不是index.jsp)才会给出404。
我现在感到愚蠢。任何人都有任何线索可能会发生什么?
答案 0 :(得分:1)
因为地图网址为localhost:8080/myresource
而不是localhost:8080/rest/myresource
答案 1 :(得分:0)
答案 2 :(得分:0)
除了Mehraj所写的内容(使用@Path("/rest/myresource")
)之外,您可能忘记在web.xml中定义您的球衣应用程序
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.your.company.project.JerseyApplication</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.spotitube</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
在任何情况下,如果您可以在您的球衣应用程序中发布可能有帮助的相关代码。
应该这样开始:
@ApplicationPath("/")
public class JerseyApplication extends ResourceConfig {