出现404错误以运行Jersey REST [英] Getting a 404 error to run a Jersey REST
问题描述
我正在尝试运行我的第一个HelloWorld Jersey项目,阅读了很多教程,从理论上讲,它应该可以工作,但是当然我做错了什么,该页面给了我404错误. 这是我所拥有的:
我从Eclise中的DynamicWebProject开始,并使用插件将其转换为Maven项目.并将它们添加到POM文件中:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
然后,我还添加了一个类似这样的小类,以添加一些Jersey注释:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/hello")
public class HelloWorldResource {
@GET
@Produces("application/plain")
public String getMessage() {
// Forward request to service layer.
return "Hello World";
}
}
并且我还在web.XML文件中用这些文件注册了Jersey:
<servlet>
<servlet-name>Jersey REST Service</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>JerseyREST</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
注意:那里已经有一些创建的东西,我还没有碰过.他们仍然在那里.
因此,使用此配置,我在服务器上运行并转到
http://localhost:8080/JerseyREST/rest/hello
但收到令人讨厌的HTTP状态404-/JerseyREST/jerseyrest/rest/hello错误.而且我无法弄清楚我做错了什么部分. 我有什么建议或地方吗?
非常感谢.
我点击了此链接以实现我的第一个Jersey Web服务:有关.改为使用MediaType.TEXT_PLAIN
.
您需要将这些jar文件添加到/WEB-INF/lib/下:
asm-3.1
,jackson-core-asl-1.9.2
,jackson-jaxrs-1.9.2
,jackson-mapper-asl-1.9.2
,jackson-xc-1.9.2
,jersey-client-1.11
,jersey-core-1.11
,jersey-json-1.11
,jersey-server-1.11
,jersey-servlet-1.11
,jettison-1.1
和jsr311-api-1.1.1
. >
I am trying to run my first HelloWorld Jersey project ever, read bunch of tutorials on it and I think theoretically it should work but of course I am doing something wrong that the page gives me a 404 error. Here is what I have:
I started with a DynamicWebProject in Eclise and using plugins convereted it to a Maven project. And added these to the POM file:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
Then I also added a pretty small class like this to have some Jersey annotations:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/hello")
public class HelloWorldResource {
@GET
@Produces("application/plain")
public String getMessage() {
// Forward request to service layer.
return "Hello World";
}
}
and I also registered Jersey with these in web.XML file:
<servlet>
<servlet-name>Jersey REST Service</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>JerseyREST</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
NOTE: there were already some created in there, I haven't touched them yet. They are still there.
So with this configuration I did a Run On Server and went to
http://localhost:8080/JerseyREST/rest/hello
but getting a nasty HTTP Status 404 - /JerseyREST/jerseyrest/rest/hello error on that. And I can't figoure out what part I am doing wrong. Any suggesstions or places I take a look at?
Much appreciated.
I followed this link to implement my first Jersey Web Service : REST in Java I run it on Tomcat v7.0 and it worked fine. Have you tried it on Tomcat? If not, I suggest you to try it. Sometimes it happened for me that I got 404 error permanently. To fix the error I deleted Tomcat and create a new server wizard and then it works fine.
As @Tom said it may related to "application/plain"
. Use MediaType.TEXT_PLAIN
instead.
You need to add these jar files under /WEB-INF/lib/ :
asm-3.1
, jackson-core-asl-1.9.2
, jackson-jaxrs-1.9.2
, jackson-mapper-asl-1.9.2
, jackson-xc-1.9.2
, jersey-client-1.11
, jersey-core-1.11
, jersey-json-1.11
, jersey-server-1.11
, jersey-servlet-1.11
, jettison-1.1
and jsr311-api-1.1.1
.
这篇关于出现404错误以运行Jersey REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!