【问题标题】:404 error with Rest Service - Jersey/tomcat8休息服务出现 404 错误 - Jersey/tomcat8
【发布时间】:2015-09-29 23:41:49
【问题描述】:

过去几天我一直在尝试使用许多示例,但无法使 REST 服务运行。我有 tomcat8(Ubuntu 14.x)/Jersey。 有什么想法吗?

pom.xml sn-p

<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 sn-p

 <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启动没有错误:

2015 年 7 月 11 日 11:23:08.327 信息 [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 信息 [localhost-startStop-8] org.apache.catalina.startup.HostConfig.deployWAR web 部署 申请档案 /home/apcuser/tomcat8/apache-tomcat-8.0.24/webapps/ExProcess.war 有 在 1,488 毫秒内完成

休息服务类:

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://javabrains.koushik.org/courses/javaee_jaxrs/lessons/Setting-Up 现在可以了。

【问题讨论】:

  • 重新开始,去off this看看会发生什么。

标签: java rest maven tomcat jersey


【解决方案1】:
@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 is file not found 错误,您收到此错误是因为您没有将 / 附加到 @Path 注释值。

@Path("restservice") 应该是 @Path("/restservice")@Path("{name}") 应该是 @Path("/{name}")

编辑更新-

已添加@Produces(MediaType.TEXT_PLAIN)

【讨论】:

  • 这个也试过了,没用。如何判断我的类是否被tomcat加载?
  • 检查目标文件夹中的 /WEB-INF/classes 是否存在 .class 文件。第二次查看webapps文件夹,展开war看看/WEB-INF/class中是否存在.class文件
  • 嘿,您要访问的确切网址是什么?
  • 我检查了那些,课程在那里。
  • localhost:8080/ipaddr/rest/restservice/somename ?? “ipaddr”是应用上下文根名称吗??
【解决方案2】:

你尝试关注

web.xml 中的变化

  <servlet-mapping>
    <servlet-name>RestService</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

在 servlet 中

@Path("/rest/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;
        }

}

我还看到你已经评论了代码。但是你清除了 webapps 目录了吗?停止tomcat容器->进入webapps目录->删除文件夹ExProcess(不是war)->启动容器。

【讨论】:

  • Aniket,我正在关注一些示例,并且在 web.xml 中有这些条目。 please see this example
  • Aniket,servlet 将处理 /rest/restservice/,因为 url 模式是 /rest/* 并且 OP 提到了 @Path("restservice"),它实际上应该是 RestService 中的 @Path("/restservice")
  • 另外,RestService servlet 名称很好,因为 &lt;/servlet&gt;&lt;/servlet&gt; 中的 &lt;servlet-name&gt;&lt;servlet-mapping&gt; 相同,在这种情况下指向 com.sun.jersey.spi.container.servlet.ServletContainer
  • 好的,但是服务器容器如何知道RestService 类应该处理请求?如果我们指定的 serverlet 类指向 com.sun.jersey.spi.container.servlet.ServletContainer
  • 我认为它会进行扫描(从日志中可以看出)和我课堂上给出的注释。
猜你喜欢
  • 1970-01-01
  • 2018-08-02
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多