Question

I am running my project with TomCat7 with Eclipse under UBUNTU I am trying to follow a tutorial online to create a RESTful api. I follow exactly the same steps, but I get the 404 error when I try to run the project.

Here is my .class file that is in the src folder

package com.eviac.blog.restws;




import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;




/**
* 
* @author pavithra
* 
*/




// @Path here defines class level path. Identifies the URI path that 
// a resource class will serve requests for.
@Path("UserInfoService")
public class UserInfo {




// @GET here defines, this method will method will process HTTP GET
// requests.
@GET
// @Path here defines method level path. Identifies the URI path that a
// resource class method will serve requests for.
@Path("/name/{i}")
// @Produces here defines the media type(s) that the methods
// of a resource class can produce.
@Produces(MediaType.TEXT_XML)
// @PathParam injects the value of URI parameter that defined in @Path
// expression, into the method.
public String userName(@PathParam("i") String i) {




String name = i;
return "<User>" + "<Name>" + name + "</Name>" + "</User>";
}




@GET 
@Path("/age/{j}") 
@Produces(MediaType.TEXT_XML)
public String userAge(@PathParam("j") int j) {




int age = j;
return "<User>" + "<Age>" + age + "</Age>" + "</User>";
}
}

And this is web.xml that is in WEB-INF -> lib

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>RESTfulWS</display-name> 
<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>com.eviac.blog.restws</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> 
</web-app>

When I try to access this from localhost, I get the error..

The requested resource (/RESTfulWS/) is not available.

Was it helpful?

Solution

Did you check the console / logs to make sure your web app deployed and started properly in tomcat?

Did you try the (GET) request /RESTfulWS/rest/UserInfoService/name/john

OTHER TIPS

For Jersey 2.x, the servlet class for ServletContainer should be "org.glassfish.jersey.servlet.ServletContainer", the param-name for packages should be "jersey.config.server.provider.packages".

Try

<servlet> 
<servlet-name>Jersey REST Service</servlet-name> 
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
<init-param> 
<param-name>jersey.config.server.provider.packages</param-name> 
<param-value>com.eviac.blog.restws</param-value> 
</init-param> 
<load-on-startup>1</load-on-startup> 
</servlet> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top