Tomcat上的Jersey REST服务出现404错误 [英] 404 error with Jersey REST Service on Tomcat

查看:115
本文介绍了Tomcat上的Jersey REST服务出现404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了有关该主题的所有答案,或者我面临的是一个完全不同的问题,或者我错过了很多宝贵的时间.

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.

服务等级:

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

<?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>

URL:

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

应该可以,但不能.任何帮助将不胜感激.

Should work but it does not. Any help will be really appreciated.

我正在使用Jersey 2.17和Tomcat 8.0.20

I am using Jersey 2.17 and Tomcat 8.0.20

推荐答案

我正在使用Jersey 2.17"

"I am using Jersey 2.17"

在2.17版本中不存在

This does not exist in 2.17

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

我很惊讶您没有收到未找到的类异常.这意味着您可能正在混合版本.或者,也许您正在得到一个例外,那就是不告诉我们.无论如何,正确的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

org.glassfish.jersey.servlet.ServletContainer

接下来,如果有的话,您应该做的事情是除去所有以com.sun开头的程序包(jar).这些是泽西岛1罐.在第二版Jersey中,程序包的命名模式已更改为org.glassfish.xxx.如果您想使生活变得轻松,请使用Maven,然后仅向整个项目添加一个依赖项,其余的将全部引入.

Next what you should do, if you have any, is get rid of everything (jar) whose packages begin with com.sun. These are Jersey 1 jars. 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.

<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

jersey.api.json.POJOMappingFeature

在《泽西岛2》中,只需添加Maven掺杂,生活就会很好.

In Jersey 2, simply add this Maven depedency, and life will be good.

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


如果您不使用Maven,请在此处下载RI软件包.打开所有文件夹,然后将每个jar添加到您的项目中.这是对核心的支持.


If you're not using Maven, download the RI bundle here. Open all the folders and add every jar to your project. This is for core support.

要获取JSON支持,请下载,以及所有这些.您可以在同一网站上搜索它们.这样做无需额外配置.或者,您可以只下载第二个链接中的链接,然后将包添加到web.xml中,如链接中所示.

For JSON support, download this, as well as all of these. 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.

但是为了使其正常工作,由于您的代码不产生或不使用任何JSON,因此您可以简单地先使核心运行,然后一旦其开始正确运行,就可以使用JSON支持.

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.

这篇关于Tomcat上的Jersey REST服务出现404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆