Guide To Your First Restful Web Service In Java

Why ?..

Todays world  mainly focuses on Integration of applications to different platforms such as Web,Mobile & IOT etc and hunt for a single best flexible framework is indeed  challenging, In the growing new era for languages like JS and Python, Java is trying to keep up with its web-services like restful and soap.

while SOAP services mainly concentrate on XML based integration with WSDL, Restful provides wide range of support for JSON,TEXT,XML etc. So most of the mobile application backends choose Restful with JSON as standard data source type.

So if you are one of those enthusiastic person looking to learn Restful with java webservice this tutorial will be your first step.

What we do here :-
  1. We create a restful web-service in java using jersey jar files with static data.
Requirements :-
  1. Latest JDK 1.8 installed and JAVA_HOME configured to path-to/jdk8/
  2. Any Eclipse JEE IDE
  3. Jersey Jar bundle Files
Create Project :-

Open Eclipse and Goto Files -> New-> Dynamic Web Project.

Create Project Name as RestFul and click on next and check Generate web.xml finally finish.

Create index.html in WebContent and verify welcome-file list in web.xml, Finally deploy and test Index page at http://localhost:8090/RestFul/ .

RestFul/WebContent/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>hello</h4>
</body>
</html>

RestFul/WebContent/WEB-INF/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>RestFul</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>My Rest Project</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.simplepackage</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>My Rest Project</servlet-name>
  	<url-pattern>/api/*</url-pattern>
  </servlet-mapping>
</web-app>

Create com.simplepackage Package in RestFul/Java Resources/src directory

Now download and add all jersey jar files to the /RestFul/WebContent/WEB-INF/lib/

                

Add a simple Java Class file called Controller which we will be using as our rest service provider class

          

Lets create a function called getText which will return simple String ‘Hello World’

Now add annotation @GET provided by javax.ws.GET package to tell that the following function only accepts request type GET,

Moving on lets add URL mapping to our function and the Controller class with annotation  @Path and don’t forget to add @Produces annotation which indicates the the response type from your service eg:-XML,JSON & TEXT etc., for now lets stick with plain text for which we are using MediaType.PLAIN_TEXT or directly add application/json .

package com.simplepackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/Controller")
public class Controller {
  @GET
  @Path("/getText")
  @Produces(MediaType.TEXT_PLAIN)
  public String getText() {
    return "hello world";
  }
}

Now deploy application by runing project on your tomcat server and go to http://localhost:8090/RestFul/api/Controller/getText on your browser.

Now similarly add a Consumer function in the same way as above but we have to do some minor changes.

Lets just say the we pass a parameter via GET method with name param and return the same value.

Since the passing parameter is a string wi define return type as string and directly acquire parameter inside function args section with annotation @QueryParam(“param”) where param is the name of Request argument.

package com.simplepackage;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@Path("/Controller")
public class Controller {
  @GET
  @Path("/getText")
  @Produces(MediaType.TEXT_PLAIN)
  public String getText() {
    return "hello world";
  }
  
  @GET
  @Path("/setText")
  @Produces(MediaType.TEXT_PLAIN)
  public String setText(@QueryParam("param") String param) {
    return param;
  }
}

Now deploy application by runing project on your tomcat server and go to http://localhost:8090/RestFul/api/Controller/setText?param=world on your browser.

Now try exploring restful api with POST method.

297 total views, 1 views today

Posts created 6

One thought on “Guide To Your First Restful Web Service In Java

Comments are closed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top