Implementing a Tomcat RESTful Web Service
I am writing a simple RESTful web service using Java, tomcat7, jersey and eclipse IDE.
When I started the web service using eclipse (Servers) it works well. I have tested the GET and POST method. But when I export the application to a WAR file and deployed with tomcat, control the UI. It returns a 404 not found status.
Here's an 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 anyone please explain what is the difference between starting a service in eclipse and deploying to localhost (OR remote host)? And how can I debug or get some traces about this?
source to share
there are 2 suggestions for you to get rid of this problem 1) in the resource file create a default method so that if the url doesn't match it will link differently it might give a 404
@GET
@Produces({ MediaType.TEXT_HTML, MediaType.TEXT_PLAIN })
public String default() {
return "Hello Rest Api";
}
you can see -> Example of rest api resource
2) set the default 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 -> Set api default path
so when you call your api like -> http://something.com/project/rest then your resource file default method will run. so there is no 404.
source to share
I finally got it working. I am setting context-root in eclipse project properties. The available url will look something like localhost: 8080 / context-root / rest / ... But when I deploy this with a WAR file to Tomcat, this configuration is ignored. The correct url is still: localhost: 8080 / project / rest / ...
I need to find how to set context-root in web.xml or somewhere else.
source to share
The web.xml settings for running REST Api using Jersey are best explained in the following url.
http://www.vogella.com/tutorials/REST/article.html#jerseyprojectsetup_gradle
I am developing a REST Api and am following the web.xml setup.
<servlet>
<servlet-name>BOARDWALK REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>io.swagger.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>BOARDWALK REST Service</servlet-name>
<url-pattern>/bae/*</url-pattern>
</servlet-mapping>
All REST API service classes are stored in the classes / io / swagger / api folder. When I call the REST api, I use the following url and it works.
http: // localhost: 8080 / bae4_3_release / bae / bcpInstance
where http: // localhost: 8080 / bae4_3_release is the context. / bae / bcpInstance points to a class in classes / io / swagger / api / BcpInstanceApi.class that has a PATH as @bcpInstance defined in it.
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>io.swagger.api</param-value>
</init-param>
Indicates the deployment location of the REST API.
source to share