使用Tomcat在Jersey中获取404错误

时间:2016-06-02 18:48:17

标签: eclipse rest tomcat jersey-2.0

我花了几个小时的时间来完成这项工作,但仍然无法找到可行的解决方案。我已将所有jar文件移动到WEB-INF文件夹中设置构建路径。

我使用Jersey 2,在Eclipse上使用Tomcat 8。 http://localhost:8080/工作正常,并显示Tomcat页面。但是,我访问http://localhost:8080/JerseyLivin/rest/hello我收到了404错误页面 -

type Status report

message /JerseyLivin/rest/hello

description The requested resource is not available.

这是web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JerseyLivin</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>JerseyLivin</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 

java类文件,Hello.java:

package jerseyLivin;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

} 

和tomcat日志文件:

127.0.0.1 - - [02/Jun/2016:11:37:06 -0700] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [02/Jun/2016:11:37:09 -0700] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [02/Jun/2016:11:37:09 -0700] "GET /tomcat.png HTTP/1.1" 304 -
0:0:0:0:0:0:0:1 - - [02/Jun/2016:11:37:09 -0700] "GET /tomcat.css HTTP/1.1" 304 -
0:0:0:0:0:0:0:1 - - [02/Jun/2016:11:37:09 -0700] "GET /favicon.ico HTTP/1.1" 200 21630
0:0:0:0:0:0:0:1 - - [02/Jun/2016:11:37:12 -0700] "GET /JerseyLivin/rest/hello HTTP/1.1" 404 1038
0:0:0:0:0:0:0:1 - - [02/Jun/2016:11:37:12 -0700] "GET /favicon.ico HTTP/1.1" 200 21630

我有没有看到正在制作404消息的内容?

0 个答案:

没有答案