简体   繁体   English

Tomcat上的Jersey REST服务出现404错误

[英]404 error with Jersey REST Service on Tomcat

I have looked at all the answers available on this topic, either I am facing a completely different problem or I am missing something big time. 我已经查看了有关此主题的所有可用答案,或者我面临的是一个完全不同的问题,或者我错过了很多宝贵的时间。

Service Class: 服务等级:

package org.test;

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

//http://localhost:8080/JunkWeb/rest/TestRestService/callService
@Path("/TestRestService")
public class TestRestService {

    @GET
    @Path("/callService")
    @Produces(MediaType.TEXT_PLAIN)
    public String callService(){return "from Rest Service";}

}//class closing

web.xml 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" version="3.0">
  <display-name>JunkWeb</display-name>

  <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>org.test</param-value>
    </init-param>
    <!-- <init-param>
      <param-name>jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
    </init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>
   <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

The URL: 网址:

http://localhost:8080/JunkWeb/rest/TestRestService/callService

在此处输入图片说明

Should work but it does not. 应该可以,但是不能。 Any help will be really appreciated. 任何帮助将不胜感激。

I am using Jersey 2.17 and Tomcat 8.0.20 我正在使用Jersey 2.17和Tomcat 8.0.20

"I am using Jersey 2.17" “我正在使用Jersey 2.17”

This does not exist in 2.17 在2.17中不存在

com.sun.jersey.spi.container.servlet.ServletContainer

I'm surprised you're not getting a class not found exception. 我很惊讶您没有收到未找到的类异常。 Which means you are probably mixing versions. 这意味着您可能正在混合版本。 Or maybe you're getting an exception an not telling us. 或者,也许您正在得到一个例外,那就是不告诉我们。 In any case, the correct ServletContainer should be 无论如何,正确的ServletContainer应该是

org.glassfish.jersey.servlet.ServletContainer

Next what you should do, if you have any, is get rid of everything (jar) whose packages begin with com.sun . 接下来,如果有的话,应该做的事情是摆脱所有以com.sun开头的包(jar)。 These are Jersey 1 jars. 这些是球衣1罐。 In Jersey two, the package naming pattern changed to org.glassfish.xxx If you want to make life easy, use Maven, and simply add just one dependency to whole project, and it will pull in all the rest. 在第二个泽西岛,程序包的命名模式更改为org.glassfish.xxx如果您想使生活变得轻松,请使用Maven,并仅向整个项目添加一个依赖项,其余的将全部引入。

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.17</version>
</dependency>

Also, this doesn't exist in Jersey two either 而且,这在泽西岛2都不存在

jersey.api.json.POJOMappingFeature

In Jersey 2, simply add this Maven depedency, and life will be good. 在《泽西岛2》中,只需添加这种Maven优势,生活就会很好。

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.17</version>
</dependency>

If you're not using Maven, download the RI bundle here . 如果您不使用Maven,请在此处下载RI软件包。 Open all the folders and add every jar to your project. 打开所有文件夹,然后将每个jar添加到您的项目中。 This is for core support. 这是对核心的支持。

For JSON support, download this , as well as all of these . 对于JSON支持,下载 ,以及所有这些 You can search the same site for them. 您可以在同一站点上搜索它们。 doing this should work with no extra configuration. 这样做无需额外配置。 Alternatively, you can just download only the ones in the second link, then add the package in the web.xml, as seen in the link. 或者,您可以只下载第二个链接中的链接,然后将包添加到web.xml中,如链接中所示。

But just to get it working, since your code doesn't produce or consume any JSON, you can simply get the core running first, then once it starts working correctly, you can work on the JSON support. 但是仅仅为了使其正常工作,因为您的代码不会产生或使用任何JSON,因此您可以简单地先使核心运行,然后一旦其开始正确运行,就可以使用JSON支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM