Jersey Tomcat服务出现404错误 [英] Getting 404 error with Jersey Tomcat service

查看:166
本文介绍了Jersey Tomcat服务出现404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Jersey和Tomcat在Java中运行RESTful服务时,我得到404页面.

I am getting a 404 page when trying to run a RESTful service in Java with Jersey and Tomcat.

这是我的项目:

这是HelloWorld.java:

Here is the HelloWorld.java:

package service;
import javax.ws.rs.*;

@Path("/hello")
public class HelloWorld {
    @GET
    @Produces("text/plain")
    public String get() {
        return "Hello World";
    }
}

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
 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">
 <display-name>EclipseTest</display-name>
 <servlet>
  <display-name>Rest Servlet</display-name>
  <servlet-name>RestServlet</servlet-name>
  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
   <param-name>jersey.config.server.provider.packages</param-name>
   <param-value>service</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>RestServlet</servlet-name>
  <url-pattern>/service/*</url-pattern>
 </servlet-mapping>
</web-app>

我已经根据其他SO答案尝试了这些URL:

I have tried these URLs based on other SO answers:

http://localhost:8080/EclipseTest/service/hello

http://localhost:8080/service/hello

使用运行方式>在服务器上运行"时,它会将我发送到此页面(404s): http://localhost:13036/EclipseTest/WEB-INF/classes/service/HelloWorld.java

When using 'Run As > Run on Server' it sends me to this page (404s): http://localhost:13036/EclipseTest/WEB-INF/classes/service/HelloWorld.java

我正在使用:

  • Java x64 7
  • 泽西岛2.17
  • Tomcat x64 7.0.62

任何帮助将不胜感激.

推荐答案

您的路径不正确.

您的上下文路径为EclipseTest,因此,如果您想访问helloworld,则应像contextpath+path param

Your context path is EclipseTest so if you wanna access the helloworld then you should access like contextpath+path param

所以应该是

localhost:8080/EclipseTest/hello

因为您已将类映射到hello,所以contextpath + path -> EclipseTest/hello

Because you have mapped the class to hello so contextpath + path -> EclipseTest/hello

这篇关于Jersey Tomcat服务出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆