Build RESTful Web service All type data provider
This demo application will show you how to Build RESTful Web service All type data provider Using JAX-RS and Jersey and test it with java client class.
Note: If you are not familiar with RESTful web service and need depth introduction on it before developing application please read this documentation: RESTful web service introduction
In this example, the method helloWorld() is annotated with @GET and the @Produces all type of annotation. This method will process all types of requests and produce content.
Below technology are needed to build application:
- Eclipse Kepler
- Java 1.6
- Tomcat 7.0
- JAX-RS 1.1
- Jersey 1.8
- Maven 3.0
Steps:
- Create maven project name: RESTfulXMLAndJavaClient (If you are not sure how to create maven project in eclipse please follow this tutorial: Create Maven project eclipse)
- Configure Tomcat server in eclipse (If you are not sure please follow this tutorial: Configure Tomcat server in eclipse)
- Now create package name: com.javahonk inside src/main/java folder
- Create class name HelloWorld.java and copy paste below code:
package com.javahonk; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/user") public class HelloWorld { //HTML Request @GET @Path("/helloWorld") @Produces(MediaType.TEXT_HTML) public String helloWorld() { return "<html> " + "<title>" + "Hello World!" + "</title>"+ "<body><h1>" + "Hello World!" + "</body></h1>" + "</html> "; } // XML request @GET @Path("/helloWorld") @Produces(MediaType.TEXT_XML) public String sayXMLHello() { return "<?xml version=\"1.0\"?>" + "<hello> Hello World from RESTful. It is TEXT_XML request." + "</hello>"; } // XML request @GET @Path("/helloWorld") @Produces(MediaType.APPLICATION_XML) public String sayApplicationXMLHello() { return "<?xml version=\"1.0\"?>" + "<hello> Hello World from RESTful. It is APPLICATION_XML request." + "</hello>"; } // JASON request @GET @Path("/helloWorld") @Produces(MediaType.APPLICATION_JSON) public String sayJASONHello() { return "<?xml version=\"1.0\"?>" + "<hello> Hello World from RESTful. It is APPLICATION_JSON request." + "</hello>"; } // TEXT_PLAIN request @GET @Path("/helloWorld") @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() { return "Hello World from RESTful. It is TEXT_PLAIN request."; } }
- Now create java client class to test RESTful web service. Create class name RESTfulTestClient.java inside package test and copy paste below code:
package test; import java.net.URI; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; public class RESTfulTestClient { public static void main(String[] args) { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource service = client.resource(getBaseURI()); System.out.println(service.path("rest/user/helloWorld").accept(MediaType.TEXT_HTML).get(String.class)); System.out.println(service.path("rest/user/helloWorld").accept(MediaType.TEXT_PLAIN).get(String.class)); System.out.println(service.path("rest/user/helloWorld").accept(MediaType.TEXT_XML).get(String.class)); System.out.println(service.path("rest/user/helloWorld").accept(MediaType.APPLICATION_JSON).get(String.class)); System.out.println(service.path("rest/user/helloWorld").accept(MediaType.APPLICATION_XML).get(String.class)); } private static URI getBaseURI() { return UriBuilder.fromUri("http://localhost:8080/RestFulService/").build(); } }
- Create index.jsp file and copy paste below content:
<html> <title>Hello World!</title> <head> <meta http-equiv="content-type" content="text/xml; charset=UTF-8"> </head> <body> <h2>Show me first RESTful Hello World!</h2> <form id="userForm" action="rest/user/helloWorld" method="GET"> <table> <tr> <td><input type="submit" value="Submit" /></td> </tr> </table> </form> </body> </html>
- Now copy paste below code in your web.xml file:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>First RESTful Hello World</display-name> <servlet> <servlet-name>rest-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>com.javahonk</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
- Copy below maven dependency jar into your pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javahorn</groupId> <artifactId>RestFulService</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RestFulService Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-bundle</artifactId> <version>1.10-b01</version> </dependency> </dependencies> <build> <finalName>RestFulService</finalName> </build> </project>
- After all steps your project should look as below:
- Now refresh and clean your project.
- Deploy application on tomcat: Right click project –> Run As –> Run on Server. You will see below index.jsp page click Submit button to get response from service:
- After click submit button you will see response from service as below:
- Now let’s test this with our java client RESTfulTestClient class. Run java test class: Right click java class –>Run As –> Java application. You will see below on your console. Great our test got successful.
Download project: RESTfulXMLAndJavaClient