[英]HTTP Status 404 when testing restful webservice using Jersey
I have been trying to develop and deploy a restful web service using Jersey. 我一直在尝试使用Jersey开发和部署一个安静的Web服务。 I have followed the documentation from docs.oracle.com and also various other sources but getting HTTP Status 404 upon testing the service.
我已经关注了docs.oracle.com以及其他各种来源的文档,但在测试服务时获得了HTTP状态404。
Following is my 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Hello World</display-name>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.rest.jersey.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>
And this is my service. 这是我的服务。
package com.rest.jersey.test;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class HelloWorld {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String response(){
return "Hello World!";
}
}
When I try to hit the URL 当我尝试点击URL时
http://localhost:8080/TestRestWithJersey/resources/hello
it gives HTTP Status 404 error. 它给出HTTP状态404错误。 Please help me regarding this.
请帮我解决这个问题。
I just checked log file of Eclipse and it doesn't say anything other than this: 我刚刚检查了Eclipse的日志文件,除了这个之外它没有说什么:
!MESSAGE No projects found for [/workspace/TestRestWithJersey/WebContent/WEB-INF/web.xml]
!MESSAGE没有找到[/workspace/TestRestWithJersey/WebContent/WEB-INF/web.xml]的项目
I didn't understand the message, can you help me on this? 我不明白这个消息,你能帮帮我吗?
Also, the tomcat log when hitting url says 此外,击中网址时的tomcat日志说
SEVERE: Error loading WebappClassLoader
context: /TestRestWithJersey
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@5362ab
com.sun.jersey.spi.container.servlet.ServletContainer
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
I added jersey-server.jar along with jsr-311.jar 我添加了jersey-server.jar和jsr-311.jar
Also tried earlier with single jersey-bundle.jar 之前还尝试过单一的jersey-bundle.jar
The problem is still same. 问题仍然存在。
Any help is appreciated. 任何帮助表示赞赏。
When you want to use a servlet container to deploy your application you also need to add jersey-servlet (see User Guide ) module as a dependency of your project. 如果要使用servlet容器部署应用程序,还需要添加jersey-servlet (请参阅用户指南 )模块作为项目的依赖项。 If you're using maven, simply put
如果你正在使用maven,那就简单地说吧
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.15</version>
</dependency>
into your pom.xml
file. 进入你的
pom.xml
文件。
If you want to use jersey-bundle instead please make sure you have asm on your classpath because package scanning (triggered by com.sun.jersey.config.property.packages
init-param in your web.xml
) needs this library to function properly. 如果你想使用jersey-bundle,请确保你的类路径中有asm ,因为包扫描(由
web.xml
的com.sun.jersey.config.property.packages
init-param触发)需要此库正常运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.