本文介绍了REST服务出现404错误-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:

其他服务类别:

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://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-UpIt 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 22:31