0

I have been trying this for the past couple of days with many examples, I am not able to make the REST service running. I have tomcat8(Ubuntu 14.x)/Jersey. Any idea?

pom.xml snippet

<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.18.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.18.1</version>
    </dependency>
</dependencies>

web.xml snippet

 <servlet>
    <servlet-name>RestService</servlet-name>
    <!--servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class-->
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
             <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
<!--param-name>jersey.config.server.provider.packages</param-name-->
        <param-value>mail.service</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>RestService</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

No errors on tomcat startup:

11-Jul-2015 11:23:08.327 INFO [localhost-startStop-8] com.sun.jersey.server.impl.application.WebApplicationImpl._initiate Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM' 11-Jul-2015 11:23:08.795 INFO [localhost-startStop-8] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /home/apcuser/tomcat8/apache-tomcat-8.0.24/webapps/ExProcess.war has finished in 1,488 ms

Rest service class:

package mail.service;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

@Path("restservice")
public class RestService {
        @Context
        UriInfo uriInfo;
        @Context
        Request request;
        String id;

        @GET
        @Path("{name}")
        public String sayHello(@PathParam("name") String name){
            return "Hello " + name;
        }

}

Result:

HTTP Status 404 - /rest/restservice/TestName

Update: I couldn't figure out whats wrong with my project, just started from scratch following : http://javabrains.koushik.org/courses/javaee_jaxrs/lessons/Setting-Up It works now.

Sathish Kumar
  • 313
  • 2
  • 15

2 Answers2

1
@Path("/restservice")
public class RestService {
        @Context
        UriInfo uriInfo;
        @Context
        Request request;
        String id;

        @GET
        @Path("/{name}")
        @Produces(MediaType.TEXT_PLAIN)
        public String sayHello(@PathParam("name") String name){
            return "Hello " + name;
        }

}

http 404 is file not found error, you are getting this because you didn't append / to the @Path annotation value.

@Path("restservice") should be @Path("/restservice") and @Path("{name}") should be @Path("/{name}")

EDIT Update-

added @Produces(MediaType.TEXT_PLAIN)

Sudhansu Choudhary
  • 3,322
  • 3
  • 19
  • 30
-1

You try following

Changes in web.xml

  <servlet-mapping>
    <servlet-name>RestService</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

and in servlet

@Path("/rest/restservice/")
public class RestService {
        @Context
        UriInfo uriInfo;
        @Context
        Request request;
        String id;

        @GET
        @Path("/{name}/")
        public String sayHello(@PathParam("name") String name){
            return "Hello " + name;
        }

}

Also I see you have commented the code. But have you cleared webapps directory? Stop the tomcat container -> Go to webapps directory -> Delete folder ExProcess (not war) -> Start container.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Aniket, I was following some samples and in that web.xml had these entries. [please see this example](http://www.java4s.com/web-services/jersey-hello-world-example-using-jax-rs-specification/) – Sathish Kumar Jul 11 '15 at 07:42
  • Aniket, the servlet will handle `/rest/restservice/` as the url pattern is `/rest/*` and OP has mentioned `@Path("restservice")` which actually should be `@Path("/restservice")` in RestService. – Sudhansu Choudhary Jul 11 '15 at 07:47
  • Also, `RestService` servlet name is fine as far as `` in `` and `` are same, in this case pointing to `com.sun.jersey.spi.container.servlet.ServletContainer` – Sudhansu Choudhary Jul 11 '15 at 07:52
  • Ok but how will server container know `RestService ` class should handle the request? if serverlet class we specifiy points to `com.sun.jersey.spi.container.servlet.ServletContainer` ? – Aniket Thakur Jul 11 '15 at 08:19
  • I thought it does a scanning (evident from the logs) and the annotations given in my class. – Sathish Kumar Jul 11 '15 at 08:32
  • Yup you have provided it `mail.service`, So your package `mail.service` will be scanner which does have RestService class. – Aniket Thakur Jul 11 '15 at 08:49
  • @user1882828 I see you have commented the code. But have you cleared webapps directory? Stop the tomcat container -> Go to webapps directory -> Delete folder ExProcess (not war) -> Start container. – Aniket Thakur Jul 11 '15 at 08:51