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>
网址:
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
我很惊讶你没有得到 class not found 异常.这意味着您可能正在混合版本.或者也许你得到了一个例外而不是告诉我们.无论如何,正确的 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 2 中,包命名模式更改为 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
在 Jersey 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.
相关文章