REST服务出现404错误-Jersey/tomcat8 [英] 404 error with Rest Service - Jersey/tomcat8
问题描述
过去几天来我一直在尝试使用许多示例,但是我无法使REST服务运行.我有tomcat8(Ubuntu 14.x)/Jersey. 有什么主意吗?
I have been trying this for the past couple of days with many examples, I am not able to make the REST service running. I have tomcat8(Ubuntu 14.x)/Jersey. Any idea?
pom.xml代码段
pom.xml snippet
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18.1</version>
</dependency>
</dependencies>
web.xml代码段
web.xml snippet
<servlet>
<servlet-name>RestService</servlet-name>
<!--servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class-->
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<!--param-name>jersey.config.server.provider.packages</param-name-->
<param-value>mail.service</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
tomcat启动没有错误:
No errors on tomcat startup:
2015年7月11日11:23:08.327 INFO [localhost-startStop-8] com.sun.jersey.server.impl.application.WebApplicationImpl._initiate 正在启动Jersey应用程序,版本为"Jersey:1.18.1 02/19/2014" 03:28 AM'2015年7月11日11:23:08.795 INFO [localhost-startStop-8] org.apache.catalina.startup.HostConfig.deployWAR部署Web 应用档案 /home/apcuser/tomcat8/apache-tomcat-8.0.24/webapps/ExProcess.war具有 在1488毫秒内完成
11-Jul-2015 11:23:08.327 INFO [localhost-startStop-8] com.sun.jersey.server.impl.application.WebApplicationImpl._initiate Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM' 11-Jul-2015 11:23:08.795 INFO [localhost-startStop-8] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /home/apcuser/tomcat8/apache-tomcat-8.0.24/webapps/ExProcess.war has finished in 1,488 ms
其他服务类别:
package mail.service;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
@Path("restservice")
public class RestService {
@Context
UriInfo uriInfo;
@Context
Request request;
String id;
@GET
@Path("{name}")
public String sayHello(@PathParam("name") String name){
return "Hello " + name;
}
}
结果:
HTTP状态404-/rest/restservice/TestName
HTTP Status 404 - /rest/restservice/TestName
更新:我无法弄清楚我的项目有什么问题,只是从头开始以下内容: http://javabrains.koushik.org/courses/javaee_jaxrs/lessons/Setting-Up 现在可以使用了.
Update: I couldn't figure out whats wrong with my project, just started from scratch following : http://javabrains.koushik.org/courses/javaee_jaxrs/lessons/Setting-Up It works now.
推荐答案
@Path("/restservice")
public class RestService {
@Context
UriInfo uriInfo;
@Context
Request request;
String id;
@GET
@Path("/{name}")
@Produces(MediaType.TEXT_PLAIN)
public String sayHello(@PathParam("name") String name){
return "Hello " + name;
}
}
http 404是找不到文件错误,之所以出现此错误,是因为您没有将/
附加到@Path
批注值中.
http 404 is file not found error, you are getting this because you didn't append /
to the @Path
annotation value.
@Path("restservice")
应该是@Path("/restservice")
,@Path("{name}")
应该是@Path("/{name}")
编辑更新-
添加了@Produces(MediaType.TEXT_PLAIN)
这篇关于REST服务出现404错误-Jersey/tomcat8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!