Bohn
Bohn

Reputation: 26919

Getting a 404 error to run a Jersey REST

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.

Upvotes: 3

Views: 4127

Answers (2)

Varun
Varun

Reputation: 1

 <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>JerseyREST</param-value>

the param value should point to the folder where you have created the class.

Upvotes: 0

Ali
Ali

Reputation: 9994

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.

Upvotes: 1

Related Questions