Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat RESTful Web service deployment

I'm writing a simple RESTful web service using Java, tomcat7, jersey and the IDE eclipse.

When I launched the web service using eclipse (Servers), it works well. I tested the GET and POST method. But when I export the application in WAR file and deployed with tomcat manage UI. It returns status 404 not found.

Here is the example:

@Path("/webservice")
public class WebService {

    @POST
    @Path("/post")
    @Produces(MediaType.APPLICATION_JSON)
    public Response helloWorld(String inputJson) {
        return Response.ok().entity("Hello World").build();
    }

    @GET  
    @Path("/{param}")  
    public Response getMessage(@PathParam("param") String message) {  
        String output = "Jersey say Hello World!!! : " + message;  
        return Response.status(200).entity(output).build();  
    }
}

Here is the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>WebService</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>jersey-servlet</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>package.webservice</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

Can anybody explain what's the difference between the launch the service in eclipse and deploy in localhost (OR remote host)? And how can I debug or get some traces about this?

like image 827
Tony DING Avatar asked Oct 17 '25 11:10

Tony DING


2 Answers

there are 2 suggestion for you to get rid from this problem 1) in your resource file make a default method so that if no url match then it will invoke otherwise it may give 404

@GET
@Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN })
public String default() {
    return "Hello Rest Api";
}

you can see -> Rest api resource example

2) set a default rest api path in your web.xml lke below

<servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

you can see -> Rest api default path set

so that when you call your api like -> http://something.com/project/rest then your default method of the resource file will fire. so no 404 happen.

like image 147
M. M. Saleh Avatar answered Oct 19 '25 00:10

M. M. Saleh


I finally get it working. I set the context-root in the properties of the eclipse project. The URL accessible will be something like: localhost:8080/context-root/rest/... But when I deploy this with WAR file in Tomcat, this configuration is not taken into account. The correct URL is still: localhost:8080/project/rest/...

I have to find how to set the context-root in web.xml or somewhere else.

like image 37
Tony DING Avatar answered Oct 19 '25 00:10

Tony DING