0
votes

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.

2
Start over and go off this and see what happens.Paul Samsotha

2 Answers

1
votes
@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)

-1
votes

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.