【发布时间】:2016-07-15 23:01:36
【问题描述】:
我知道这个标题好像很多人以前都问过这个问题,但我做了很多研究,找不到任何解决方案可以解决我的问题。我在这个问题上花了两天时间,所以请帮帮我!
我正在尝试使用 Eclipse Kepler Service Release 2、Jersey、Eclipse 内置 maven、Servlet 3.0 和 Tomcat 7.0 构建一个简单的“hello world”类型的 restful web 服务。当我使用“运行方式”->“在服务器上运行”运行我的服务时,加载默认的“index.jsp”页面没有问题,但是在加载我的服务时我不断收到 HTTP 404 错误。
This image shows my project layout.
我实现Application的类如下:
package com.ys.java.ws;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.ys.java.ws.resources.MyService;
@ApplicationPath("/ys")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(MyService.class);
return s;
}
}
我的资源类是:
package com.ys.java.ws.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/test")
public class MyService {
public MyService() {
}
@GET
@Path("/hello")
//@Produces(MediaType.TEXT_PLAIN)
@Produces("text/html")
public String getTestMsg() {
return "It works";
}
}
我的 pom.xml 文件是:
<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.ys.java.ws</groupId>
<artifactId>ysNoWebXML</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ysNoWebXML Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Servlet 3.0 API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!--JAX-rs web service API -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.1</version>
</dependency>
</dependencies>
<build>
<finalName>TestService</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的 web.xml 是:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Servlet 3.0 Web Application</display-name>
<servlet>
<servlet-name>com.ys.java.ws.MyApplication</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>com.ys.java.ws.MyApplication</servlet-name>
<url-pattern>/rest</url-pattern>
</servlet-mapping>
</web-app>
请注意:在 MyApplication 类中,我设置了 @ApplicationPath("/ys"),在 web.xml 中我设置了 <url-pattern>/rest</url-pattern>。我故意这样做,因为根据jax-rs document,
If both the <servlet-mapping> and @ApplicationPath are specified, the <servlet-mapping> takes precedence.
我想测试是否是这种情况,但无论我使用哪个路径,我都会收到 HTTP 404 错误。
【问题讨论】:
-
我看不到您使用什么 URL 来测试访问正在运行的应用程序。
-
我没有足够的积分来添加超过 2 个网址。所以...我测试了以下内容:localhost:8080/TestService/rest/test/hello ---- get 404, localhost:8080/TestService/ys/test/hello --- get 404, localhost:8080/TestService --- 工作,加载默认 index.jsp
标签: eclipse maven tomcat7 jersey-2.0 servlet-3.0