运行Jersey项目(Rest Web服务)到tomcat

我试着遵循本指南: 教程,但是当我在Web浏览器上执行它时会返回错误:

HTTP Status 404 - / type Status report message / description The requested resource (/rest.api/rest/hello) is not available. 

我在我的mac上的/ usr / local中安装了tomcat7,并在终端上使用startup.sh运行它。 这是我的web.xml和Hello.java

   HelloRest  Jersey REST Service com.sun.jersey.spi.container.servlet.ServletContainer  com.sun.jersey.config.property.packages rest.api  1   Jersey REST Service /rest/*  

 package rest.api; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; // POJO, no interface no extends // The class registers its methods for the HTTP GET request using the @GET annotation. // Using the @Produces annotation, it defines that it can deliver several MIME types, // text, XML and HTML. // The browser requests per default the HTML MIME type. //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 "" + " Hello Jersey" + ""; } // This method is called if HTML is request @GET @Produces(MediaType.TEXT_HTML) public String sayHtmlHello() { return " " + "" + "Hello Jersey" + "" + "

" + "Hello Jersey" + "

" + " "; } }

这是我的配置项目:

在此处输入图像描述

有什么建议?

只需在web.xml中使用以下配置即可

   ServletAdaptor com.sun.jersey.spi.container.servlet.ServletContainer 1   ServletAdaptor /rest/*  

希望这会帮助你。

试试这个。

  jersey-serlvet com.sun.jersey.spi.container.servlet.ServletContainer  com.sun.jersey.config.property.packages Your Service Package Name   com.sun.jersey.api.json.POJOMappingFeature true  1   jersey-serlvet /rest/*  

访问这个: Jersey RESTful

在更新的Jersey版本中,servlet-class和init-param发生了变化。 请尝试以下方法:

  Jersey REST Service org.glassfish.jersey.servlet.ServletContainer  jersey.config.server.provider.packages Your Service Package Name  1  

这对我使用Jersey版本:2.5.1。

  jersey-serlvet   com.sun.jersey.spi.container.servlet.ServletContainer    com.sun.jersey.config.property.packages   rest.api     com.sun.jersey.api.json.POJOMappingFeature   true   1 

  jersey-serlvet   /rest/*