Showing posts with label REST. Show all posts
Showing posts with label REST. Show all posts

Thursday, February 19, 2015

HATEOAS REST APIs with Springboot Application

Overview:

This article will look at the basic concept behind HATEOAS(HATE-OAS). Basically, the response of your API will consist of hypermedia (URLs) as the means to navigate between resources(you will realise what I mean further down the line of this article). The article by no means tries to describe about RESTful API design(i.e. root entry point of the API, how well the API is documented and so forth). However, behind every well structured and organised REST API, HATEOAS is evident.

This article tries to show a simple application using Spring's HATEOAS library to implement as application.

You can find the sample code for this application here.

Prerequisite:

To run the application, you need the following installed.
Java 8
Maven 3

The pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.2.1.RELEASE</version>
 </parent>
 
 <groupId>org.fazlan</groupId>
 <artifactId>springboot.sample.hateoas.app</artifactId>
 <name>Spring Boot Sample Hateoas Application</name>
 <version>1.0.SNAPSHOT</version>
 <description>Spring Boot Sample Hateoas Application</description>

 <properties>
  <!-- The main class to start by executing java -jar -->
  <start-class>org.fazlan.hateoas.SampleHateoasSslApplication</start-class>
  <spring.boot.version>1.2.1.RELEASE</spring.boot.version>
        <spring-hateoas.version>0.16.0.RELEASE</spring-hateoas.version>
        <maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
        <java.version>1.8</java.version>
    </properties>
 
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
            <version>${spring-hateoas.version}</version>
        </dependency>
 </dependencies>
 <build>
  <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.boot.version}</version>
   </plugin>
  </plugins>
 </build>
</project>

The REST Resources from the API:

org.fazlan.hateoas.web.comment.CommentResource.java


package org.fazlan.hateoas.web.comment;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.hateoas.ResourceSupport;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

public class CommentResource extends ResourceSupport {

    private Comment comment;
    private String text;

    @JsonCreator
    public CommentResource(@JsonProperty("text") String text) {
        this.text = text;
    }

    public CommentResource(Comment comment) {
        this.comment = comment;
    }

    public CommentResource withSefRef() {
        add(linkTo(methodOn(CommentController.class).getComment(comment.getId())).withSelfRel());
        return this;
    }

    public CommentResource withRel(String rel) {
        add(linkTo(methodOn(CommentController.class).getComment(comment.getId())).withRel(rel));
        return this;
    }

    public String getText() {
        if (text != null)
            return text;
        return comment.getText();
    }
}

org.fazlan.hateoas.web.post.PostResource.java


package org.fazlan.hateoas.web.post;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceSupport;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

public class PostResource extends ResourceSupport {

    private String content;
    private Post post;

    @JsonCreator
    public PostResource(@JsonProperty("content") String content) {
        this.content = content;
    }

    public PostResource(Post post) {
        this.post = post;
        add(linkTo(methodOn(PostController.class).getPost(post.getPid())).withSelfRel());
    }

    public String getContent() {
        if (content != null)
            return content;
        return post.getContent();
    }

    public Link getComments() {
        return  linkTo(methodOn(PostController.class).getComments(post.getPid())).withRel("comments");
    }
}

The REST Controllers:

org.fazlan.hateoas.web.post.PostController.java


UrlMethodDescription
/postsPOSTCreates a post
/posts/{id}/commentsPOSTAdds a comment to a given post
/postsGETLists all posts
/posts/{id}GETRetrieves a given post
/posts/{id}/commentsGETLists all comments associated with a given post


package org.fazlan.hateoas.web.post;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.fazlan.hateoas.repo.CommentRepo;
import org.fazlan.hateoas.repo.PostRepo;
import org.fazlan.hateoas.web.comment.CommentResource;

import java.util.List;

import static java.util.stream.Collectors.toList;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@Controller
@RequestMapping("/posts")
public class PostController {

    @Autowired
    private PostRepo repo;

    @Autowired
    private CommentRepo commentRepo;

    @RequestMapping(method = POST)
    @ResponseBody
    public Link createPost(@RequestBody PostResource post) {
        return getPost(repo.create(post)).getId();
    }

    @RequestMapping(value = "/{id}", method = GET)
    @ResponseBody
    public PostResource getPost(@PathVariable("id") Integer id) {
        return new PostResource(repo.get(id));
    }

    @RequestMapping(method = GET)
    @ResponseBody
    public List<Link> listPosts() {
        return repo.list().stream()
                .map(PostResource::new)
                .map(PostResource::getId)
                .collect(toList());
    }

    @RequestMapping(value = "/{id}/comments", method = POST)
    @ResponseBody
    public PostResource addComment(@PathVariable("id") Integer postId, @RequestBody CommentResource comment) {
        repo.get(postId).addComment(commentRepo.get(commentRepo.create(comment)));
        return getPost(postId);
    }

    @RequestMapping(value = "/{id}/comments", method = GET)
    @ResponseBody
    public List<Link> getComments(@PathVariable("id") Integer postId) {
        return repo.get(postId).getComments()
                .stream()
                .map(CommentResource::new)
                .map(cr -> cr.withRel("comment").getLink("comment"))
                .collect(toList());
    }
}

org.fazlan.hateoas.web.comment.CommentController.java

UrlMethodDescription
/commentsGETLists all comments
/comments/{id}GETRetrieves a given comment


package org.fazlan.hateoas.web.comment;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.fazlan.hateoas.repo.CommentRepo;

import java.util.List;

import static java.util.stream.Collectors.toList;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentRepo repo;

    @RequestMapping(method = GET)
    @ResponseBody
    public List<Link> listComments() {
        return repo.list().stream()
                .map(CommentResource::new)
                .map(CommentResource::withSefRef)
                .map(CommentResource::getId)
                .collect(toList());
    }

    @RequestMapping(value = "/{id}", method = GET)
    @ResponseBody
    public CommentResource getComment(@PathVariable("id") Integer id) {
        return new CommentResource(repo.get(id)).withSefRef();
    }
}

Running the Application

$ mvn clean spring-boot:run

Access the application at https://localhost:8443/posts Initially, this will return an empty list as there will be no posts. Lets create some resources now.

Running the Application
I'have used Advanced Rest Client Google plugin to create some resources as shown in the snapshots

1. Creating a Post

2. Creating several associated Comment resources


Navigating through the newly created resources via the hypermedia

1. Listing the Posts

2. Retrieving a given Post

3. Listing all Comments associated with a given Post

4. Retrieving a given Comment associated with a given Post


Summary

This article was a briefing of how to use Spring's HATEOAS with spring-boot to write a very simple RESTful API. Hope this will encourage you to explore more on HATEOAS and whether it suites your needs when writing well designed RESTful APIs.

You can find the sample code for this application here.

Sunday, August 24, 2014

Utterlyidle - RESTful Framework for Java Part 1/2

Overview:
This article looks at Utterlyidle, Google Code framework for writing RESTful services in Java. Utterlyidle is written based on totallylazy framework for functional programming. This is a good candidate for writing micro-services in Java.

Here, we will look at a simple example how you could leverage the Utterlyidle as a RESTful framework. The sample will demonstrate two aspects. Firstly, the server API and secondly, the client API usage to access the REST resources. This article will focus on the service API.

You can learn more about this framework here.

A comprehensive sample code for this article can be found here.

Project Structure:


 utterlyidle-sample-rest-app  
 ├── pom.xml  
 └── src  
   ├── main  
   │   ├── java  
   │   │   └── org  
   │   │     └── fazlan  
   │   │       ├── DemoApplication.java  
   │   │       ├── audit  
   │   │       │   ├── AuditHandler.java  
   │   │       │   └── AuditModule.java  
   │   │       ├── common  
   │   │       │   ├── CommonModule.java  
   │   │       │   ├── Json.java  
   │   │       │   ├── JsonResponseRenderer.java  
   │   │       │   └── Repository.java  
   │   │       ├── feed  
   │   │       │   ├── Feed.java  
   │   │       │   ├── FeedActivator.java  
   │   │       │   ├── FeedModule.java  
   │   │       │   ├── FeedRepository.java  
   │   │       │   ├── FeedResource.java  
   │   │       │   ├── Feeds.java  
   │   │       │   └── InMemoryFeedRepository.java  
   │   │       ├── framework  
   │   │       │   ├── CreateResponseFactory.java  
   │   │       │   ├── OkResponseFactory.java  
   │   │       │   └── ResponseFactory.java  
   │   │       └── greet  
   │   │         ├── GreetModule.java  
   │   │         └── GreetResource.java  
   │   └── resources  
   └── test  
     └── java  
       └── org  
         └── fazlan  
           ├── feed  
           │   ├── FeedActivatorTest.java  
           │   ├── FeedRepositoryTest.java  
           │   ├── FeedResourceAcceptanceTests.java  
           │   ├── InMemoryFeedRepositoryTest.java  
           │   └── ObjectMother.java  
           ├── greet  
           │   └── GreetResourceAcceptanceTests.java  
           └── testframework  
             ├── AcceptanceTests.java  
             ├── BDD.java  
             ├── RestClient.java  
             └── utterlyidle  
               ├── Function.java  
               └── UtterlyIdleRestClient.java  

Step 1: Creating the Project


 mvn archetype:generate -DartifactId=utterlyidle-sample-rest-app -DgroupId=org.fazlan -Dversion=1.0-SNAPSHOT -DinteractiveMode=false  

Step 2: Updating the Maven Dependencies


 <?xml version="1.0" encoding="UTF-8"?>  
 <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/xsd/maven-4.0.0.xsd">  
   <modelVersion>4.0.0</modelVersion>  
   <groupId>org.fazlan</groupId>  
   <artifactId>utterlyidle-sample-rest-app</artifactId>  
   <version>1.0-SNAPSHOT</version>  
   <repositories>  
     <repository>  
       <id>Utterlyidle - Bodar Repository</id>  
       <url>http://repo.bodar.com/</url>  
     </repository>  
   </repositories>  
   <properties>  
     <mvn.dep.googlecode.utterlyidle.version>742</mvn.dep.googlecode.utterlyidle.version>  
     <mvn.dep.googlecode.gson.version>2.2.4</mvn.dep.googlecode.gson.version>  
     <mvn.dep.testng.version>6.8.8</mvn.dep.testng.version>  
     <mvn.dep.hamcrest-core.version>1.3.RC2</mvn.dep.hamcrest-core.version>  
     <mvn.dep.mockito-all.version>1.9.5</mvn.dep.mockito-all.version>  
     <mvn.plug.failsafe.version>2.17</mvn.plug.failsafe.version>  
   </properties>  
   <dependencies>  
     <dependency>  
       <groupId>com.googlecode.utterlyidle</groupId>  
       <artifactId>utterlyidle</artifactId>  
       <version>${mvn.dep.googlecode.utterlyidle.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>com.google.code.gson</groupId>  
       <artifactId>gson</artifactId>  
       <version>${mvn.dep.googlecode.gson.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.testng</groupId>  
       <artifactId>testng</artifactId>  
       <version>${mvn.dep.testng.version}</version>  
       <scope>test</scope>  
     </dependency>  
     <dependency>  
       <groupId>org.hamcrest</groupId>  
       <artifactId>hamcrest-core</artifactId>  
       <version>${mvn.dep.hamcrest-core.version}</version>  
       <scope>test</scope>  
     </dependency>  
     <dependency>  
       <groupId>org.mockito</groupId>  
       <artifactId>mockito-all</artifactId>  
       <version>${mvn.dep.mockito-all.version}</version>  
       <scope>test</scope>  
     </dependency>  
   </dependencies>  
   <build>  
     <plugins>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-failsafe-plugin</artifactId>  
         <version>${mvn.plug.failsafe.version}</version>  
         <executions>  
           <execution>  
             <id>acceptance-test</id>  
             <phase>post-integration-test</phase>  
             <goals>  
               <goal>integration-test</goal>  
               <goal>verify</goal>  
             </goals>  
             <configuration>  
               <includes>  
                 <include>**/*AcceptanceTests.java</include>  
               </includes>  
             </configuration>  
           </execution>  
         </executions>  
       </plugin>  
     </plugins>  
   </build>  
 </project>  

Step 3: Creating the RESTful Endpoint
This is our first service containing a simple endpoint to elaborate the usage of the server API.

 package org.fazlan.greet;  
 
 import com.googlecode.utterlyidle.Response;  
 import com.googlecode.utterlyidle.annotations.GET;  
 import com.googlecode.utterlyidle.annotations.Path;  
 import com.googlecode.utterlyidle.annotations.PathParam;  
 import com.googlecode.utterlyidle.annotations.Produces; 
 
 import static com.googlecode.utterlyidle.MediaType.APPLICATION_JSON;  
 import static java.lang.String.format;  
 import static org.fazlan.framework.OkResponseFactory.ok;
  
 @Path("/greet")  
 @Produces(APPLICATION_JSON)  
 public class GreetResource {  

   @GET  
   @Path("hello/{name}")  
   public Response hello(@PathParam("name") final String name) {  
     return ok(format("Hello %s", name));  
   }  
 }  

Step 4: Creating an Application Module
The above rest service accepts a parameter called name, and returns a greeting message with that name. This service is then wired to the application via a module. Modules are the building blocks of a Utterlyidle application (Modular architecture - vertical silos compared to typical horizontal slices).

 package org.fazlan.greet;  

 import com.googlecode.utterlyidle.Resources;  
 import com.googlecode.utterlyidle.modules.ResourcesModule;  
 import static com.googlecode.utterlyidle.annotations.AnnotatedBindings.annotatedClass;  

 public class GreetModule implements ResourcesModule {  

   @Override  
   public Resources addResources(Resources resources) throws Exception {  
     return resources.add(annotatedClass(GreetResource.class));  
   }  
 }  

We create a module called GreetModule, and wire all the components related to that module.

Step 5: Adding a Module to the Application
Once the modules are created and ready, we can add them as building blocks of our application. One or more modules creates the entire application.

 package org.fazlan; 
 
 import com.googlecode.utterlyidle.Application;  
 import com.googlecode.utterlyidle.RestApplication;  
 import org.fazlan.audit.AuditModule;  
 import org.fazlan.greet.GreetModule;  

 import static com.googlecode.utterlyidle.ApplicationBuilder.application;  
 import static com.googlecode.utterlyidle.BasePath.basePath;  

 public class DemoApplication extends RestApplication {  
   public DemoApplication(String basePath) {  
     super(basePath(basePath));  
     addModules(this);  
   }  

   private void addModules(Application application) {  
     addInfrastructureModule(application);  
     addApplicationModules(application);  
   }  

   private void addInfrastructureModule(Application application) {  
     application.add(new AuditModule());  
   }  

   private void addApplicationModules(Application application) {  
     application.add(new GreetModule());  
   }  

   . . .  
 }  

Step 6: Running the Application
Once all the modules are added to the application, running the application is pretty simple. For this tutorial, we will run the application as a standalone server. This can be achieved as follows,

 package org.fazlan;  

 import static com.googlecode.utterlyidle.ApplicationBuilder.application;  
 import static com.googlecode.utterlyidle.BasePath.basePath;  

 public class DemoApplication extends RestApplication {  
   public DemoApplication(String basePath) {  
     super(basePath(basePath));  
     addModules(this);  
   }  

   . . .  

   public static void main(String[] args) throws Exception {  
     application(new DemoApplication("/")).start(7000);  
   }  
 }  

This would start the server at http://localhost:7000/. Greeting REST endpoint can be accessed at http://localhost:7000/greet/hello/

The endpoint can be accessed either,
http://localhost:7000/greet/hello/FooName or http://localhost:7000/greet/hello?name=FooName

Summary
This article briefly touch based on the capabilities provided by Utterlyidle based on totallylazy in building REST services.

You can learn more about this framework here.

A comprehensive sample code for this article can be found here.

Saturday, December 1, 2012

RESTful with RESTEasy - Part 2/4 (Client Consuming a POST and GET RESTful Service)

Overview:
This post looks at how to write a service client using RESTEasy framework, which is an excellent framework for developing RESTful services, and has gained a lot of popularity in the Mobile application development world.

Also Refer to,
RESTful with RESTEasy - Part 1/4 (Creating a POST and GET RESTful Service)

You can find the sample code here.

Prerequisite:JDK >= 1.5.x
Maven >= 2.2.x


Step 1: Maven Dependencies (pom.xml)
Update your client project's pom.xml by adding the following RESTEasy dependency.

 <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">  
   . . .
   <dependencies>  
     . . .  
     <dependency>  
       <groupId>org.jboss.resteasy</groupId>  
       <artifactId>resteasy-jaxrs</artifactId>  
       <version>${resteasy.version}</version>  
     </dependency>  
    . . .
   </dependencies>  
   . . .
 </project>  

Step 2: How to create a requests

   private static final String SERVICE_URL = "http://localhost:8080/greetingspring";  
   private static final String SERVICE_URL_ADD = "/add";  
   private static final String SERVICE_URL_UPDATED = "/update";  
   private static final String SERVICE_URL_GET = "/get/{id}";  
   private static final String SERVICE_URL_GET_ALL = "/getAll";  
   . . .  
   private static ClientRequest createRequest(String uri) {  
     ClientRequest request = new ClientRequest(SERVICE_URL + uri);  
     request.accept("application/json");  
     return request;  
   }  
   . . .  
   ClientRequest request = createRequest(SERVICE_URL_ADD)  
   ClientRequest request = createRequest(SERVICE_URL_GET)  
   ClientRequest request = createRequest(SERVICE_URL_UPDATE)  
   ClientRequest request = createRequest(SERVICE_URL_GET_ALL)  

Creating a POST request
 private static Long addUser(User newUser) throws Exception {  
     String requestJson = "{\"user\":" + JSON_CONVERTER.toJson(newUser) + "}";  
     displayRequest(requestJson);  
     ClientRequest request = createRequest(SERVICE_URL_ADD).body(CONTENT_TYPE, requestJson);  
     ClientResponse<String> response = request.post(String.class);  
     displayResponse(response, HTTP_CODE_CREATED);  
     return Long.parseLong(response.getEntity().toString());  
   }  

Creating a GET request
 private static User getUser(Long userId) throws Exception {  
     ClientRequest request = createRequest(SERVICE_URL_GET.replace("{id}", userId.toString()));  
     ClientResponse<String> response = request.get(String.class);  
     displayResponse(response, HTTP_CODE_OK);  
     String responseJson = response.getEntity().toString();  
     responseJson = responseJson.replace("{\"user\":", "").replace("}}", "}");  
     return JSON_CONVERTER.fromJson(responseJson, User.class);  
   }  

Complete Code for the Client


 package org.fazlan.resteasy.spring.client;  
 import com.google.gson.Gson;  
 import org.fazlan.resteasy.spring.entity.User;  
 import org.jboss.resteasy.client.ClientRequest;  
 import org.jboss.resteasy.client.ClientResponse;  
 import java.io.BufferedReader;  
 import java.io.ByteArrayInputStream;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  

 public class RestClient {  

   private static final String SERVICE_URL = "http://localhost:8080/greetingspring";  
   private static final String SERVICE_URL_ADD = "/add";  
   private static final String SERVICE_URL_UPDATED = "/update";  
   private static final String SERVICE_URL_GET = "/get/{id}";  
   private static final String SERVICE_URL_GET_ALL = "/getAll";  
   private static final String CONTENT_TYPE = "application/json";  
   private static final Gson JSON_CONVERTER = new Gson();  
   private static final int HTTP_CODE_OK = 200;  
   private static final int HTTP_CODE_CREATED = 201;  

   public static void main(String[] args) throws Exception {  
     User newUser = new User("Firstname", "Lastname");  
     Long userId = addUser(newUser);  
     User user = getUser(userId);  
     displayUser(user);  
     user.setFirstName("Updated " + user.getFirstName());  
     userId = updateUser(user);  
     user = getUser(userId);  
     displayUser(user);  
     addUser( new User("AnotherFirstName", "AnotherLastName"));  
     getAllUsers();  
   }  
   private static Long addUser(User newUser) throws Exception {  
     String requestJson = "{\"user\":" + JSON_CONVERTER.toJson(newUser) + "}";  
     displayRequest(requestJson);  
     ClientRequest request = createRequest(SERVICE_URL_ADD).body(CONTENT_TYPE, requestJson);  
     ClientResponse<String> response = request.post(String.class);  
     displayResponse(response, HTTP_CODE_CREATED);  
     return Long.parseLong(response.getEntity().toString());  
   }  
   private static Long updateUser(User user) throws Exception {  
     String requestJson = "{\"user\":" + JSON_CONVERTER.toJson(user) + "}";  
     displayRequest(requestJson);  
     ClientRequest request = createRequest(SERVICE_URL_UPDATED).body(CONTENT_TYPE, requestJson);  
     ClientResponse<String> response = request.post(String.class);  
     displayResponse(response, HTTP_CODE_OK);  
     return Long.parseLong(response.getEntity().toString());  
   }  
   private static User getUser(Long userId) throws Exception {  
     ClientRequest request = createRequest(SERVICE_URL_GET.replace("{id}", userId.toString()));  
     ClientResponse<String> response = request.get(String.class);  
     displayResponse(response, HTTP_CODE_OK);  
     String responseJson = response.getEntity().toString();  
     responseJson = responseJson.replace("{\"user\":", "").replace("}}", "}");  
     return JSON_CONVERTER.fromJson(responseJson, User.class);  
   }  
   private static void getAllUsers() throws Exception {  
     ClientRequest request = createRequest(SERVICE_URL_GET_ALL);  
     ClientResponse<String> response = request.get(String.class);  
     displayResponse(response, HTTP_CODE_OK);  
   }  
   private static ClientRequest createRequest(String uri) {  
     ClientRequest request = new ClientRequest(SERVICE_URL + uri);  
     request.accept("application/json");  
     return request;  
   }  
   private static void displayUser(User user) {  
     System.out.println(user);  
   }  
   private static void displayRequest(String request) {  
     System.out.println(request);  
   }  
   private static void displayResponse(ClientResponse<String> response, int expectedResponseCode) throws IOException {  
     if (response.getStatus() != expectedResponseCode) {  
       throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());  
     }  
     BufferedReader br = new BufferedReader(new InputStreamReader(  
         new ByteArrayInputStream(response.getEntity().getBytes())));  
     String output;  
     System.out.println("Output from Server .... \n");  
     while ((output = br.readLine()) != null) {  
       System.out.println(output);  
     }  
   }  
 }  

Summary:
This tutorial looked at how we can consume RESTful Web Service using RESTEasy.

You can find the sample code here.

RESTful with RESTEasy - Part 1/4 (Creating a POST and GET RESTful Service)

Overview:
This post looks at RESTEasy framework, which is an excellent framework for developing RESTful services, and has gained a lot of popularity in the Mobile application development world.


Also Refer to,
RESTful with RESTEasy - Part 2/4 (Client Consuming a POST and GET RESTful Service)


You can find the sample code here.

Prerequisite:JDK >= 1.5.x
Maven >= 2.2.x

Project Structure: Following is the project structure I have used (Maven WAR project).

mvn archetype:generate -DgroupId=org.fazlan.resteasy.spring.servic -DartifactId=org.fazlan.resteasy.spring.servic -DarchetypeArtifactId=maven-archetype-webapp -Dversion=1.0.0-SNAPSHOT -DinteractiveMode=false



Step 1: Maven Dependencies (pom.xml)

 <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>org.fazlan</groupId>  
   <artifactId>org.fazlan.resteasy.spring.service</artifactId>  
   <packaging>war</packaging>  
   <version>1.0.0</version>  
   <name>org.fazlan.resteasy.spring.service Maven Webapp</name>  
   <url>http://maven.apache.org</url>  
   <properties>  
     <resteasy.version>2.3.4.Final</resteasy.version>  
     <org.springframework.version>3.1.2.RELEASE</org.springframework.version>  
     <slf4j.version>1.6.1</slf4j.version>  
   </properties>  
   <dependencies>  
     <!-- Spring 3 dependencies -->  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-core</artifactId>  
       <version>3.1.2.RELEASE</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-web</artifactId>  
       <version>3.1.2.RELEASE</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-webmvc</artifactId>  
       <version>3.1.2.RELEASE</version>  
     </dependency>  
     <!-- jstl -->  
     <dependency>  
       <groupId>javax.servlet</groupId>  
       <artifactId>jstl</artifactId>  
       <version>1.2</version>  
     </dependency>  
     <dependency>  
       <groupId>org.jboss.resteasy</groupId>  
       <artifactId>resteasy-jaxrs</artifactId>  
       <version>${resteasy.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.jboss.resteasy</groupId>  
       <artifactId>resteasy-jaxb-provider</artifactId>  
       <version>${resteasy.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.jboss.resteasy</groupId>  
       <artifactId>resteasy-spring</artifactId>  
       <version>${resteasy.version}</version>  
       <exclusions>  
         <exclusion>  
           <artifactId>commons-logging</artifactId>  
           <groupId>commons-logging</groupId>  
         </exclusion>  
         <exclusion>  
           <artifactId>jaxb-impl</artifactId>  
           <groupId>com.sun.xml.bind</groupId>  
         </exclusion>  
         <exclusion>  
           <artifactId>sjsxp</artifactId>  
           <groupId>com.sun.xml.stream</groupId>  
         </exclusion>  
         <exclusion>  
           <artifactId>jsr250-api</artifactId>  
           <groupId>javax.annotation</groupId>  
         </exclusion>  
         <exclusion>  
           <artifactId>activation</artifactId>  
           <groupId>javax.activation</groupId>  
         </exclusion>  
       </exclusions>  
     </dependency>  
     <dependency>  
       <groupId>junit</groupId>  
       <artifactId>junit</artifactId>  
       <version>3.8.1</version>  
       <scope>test</scope>  
     </dependency>  
     <dependency>  
       <groupId>com.google.code.gson</groupId>  
       <artifactId>gson</artifactId>  
       <version>1.7.1</version>  
     </dependency>  
   </dependencies>  
   <build>  
     <finalName>UserService</finalName>  
     <plugins>  
       <plugin>  
         <groupId>org.mortbay.jetty</groupId>  
         <artifactId>maven-jetty-plugin</artifactId>  
         <version>6.1.10</version>  
         <configuration>  
           <contextPath>UserService</contextPath>  
           <scanIntervalSeconds>30</scanIntervalSeconds>  
           <stopKey>foo</stopKey>  
           <stopPort>9999</stopPort>  
         </configuration>  
       </plugin>  
     </plugins>  
   </build>  
 </project>  

Step 2: Creating POJO Classes and Service Implementation

User.java

 package org.fazlan.resteasy.spring.entity; 
 
 import javax.xml.bind.annotation.XmlRootElement;  

 @XmlRootElement  
 public class User {  

   private long id;  
   private String firstName;  
   private String lastName;  

   public User() {  
   }  

   public User(String firstName, String lastName) {  
     this.firstName = firstName;  
     this.lastName = lastName;  
   }  

   public long getId() {  
     return id;  
   }  

   @Override  
   public String toString() {  
     return "User{" +  
         "id=" + id +  
         ", firstName='" + firstName + '\'' +  
         ", lastName='" + lastName + '\'' +  
         '}';  
   }  

   public void setId(long id) {  
     this.id = id;  
   }  

   public String getFirstName() {  
     return firstName;  
   }  

   public void setFirstName(String firstName) {  
     this.firstName = firstName;  
   }  

   public String getLastName() {  
     return lastName;  
   }  

   public void setLastName(String lastName) {  
     this.lastName = lastName;  
   }  
 }  


UserService.java - The Service Interface
 package org.fazlan.resteasy.spring.service;  
 import org.fazlan.resteasy.spring.entity.User;  
 import javax.ws.rs.*;  
 import javax.ws.rs.core.MediaType;  
 import javax.ws.rs.core.Response;  

 @Path("/")  
 public interface UserService {  

   @POST  
   @Path("/add")  
   @Consumes(MediaType.APPLICATION_JSON)  
   Response add(User user);  

   @POST  
   @Path("/update")  
   @Consumes(MediaType.APPLICATION_JSON)  
   Response update(User user);  

   @GET  
   @Path("/get/{id}")  
   @Produces(MediaType.APPLICATION_JSON)  
   Response get(@PathParam("id") long id);  

   @GET  
   @Path("/getAll")  
   @Produces(MediaType.APPLICATION_JSON)  
   Response getAll();  
 }  

NOTE: This service class has been annotated to produce and consume certain methods in JSON(application/json) as the mime-type.

UserServiceImpl.java - Service Implementation
 package org.fazlan.resteasy.spring.service;  
 
 import org.fazlan.resteasy.spring.entity.User;  
 import org.springframework.stereotype.Component;  
 import javax.ws.rs.PathParam;  
 import javax.ws.rs.core.GenericEntity;  
 import javax.ws.rs.core.Response;  
 import java.util.Collection;  
 import java.util.HashMap;  
 import java.util.Map;  

 @Component  
 public class UserServiceImpl implements UserService {  

   private static long ID = 0L;  
   private static Map<Long, User> users = new HashMap<Long, User>();  

   public Response add(User user) {  
     user.setId(++ID);  
     update(user);  
     return Response.status(Response.Status.CREATED).entity(ID).build();  
   }  

   public Response update(User user) {  
     users.put(ID, user);  
     return Response.status(Response.Status.OK).entity(ID).build();  
   }  

   public Response get(@PathParam("id") long id) {  
     return Response.status(Response.Status.OK).entity(users.get(id)).build();  
   }  

   public Response getAll() {  
     GenericEntity<Collection<User>> usersEntity = new GenericEntity<Collection<User>>(users.values()) {  
     };  
     return Response.status(Response.Status.OK).entity(usersEntity).build();  
   }  
 }  

Step 3: Exposing the Service Beans as RESTful Web Service (applicationContext.xml)

 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
   <context:annotation-config/>  
   <context:component-scan base-package="org.fazlan.resteasy.spring.service"/>  
 </beans>  

NOTE: This will scan all the Spring components defined in the source and load them into the Application's Spring context.

Step 4: Updating the web.xml to register the RESTEasy Servlet.
 <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 Web Application</display-name>  
   <!--<context-param>-->  
   <!--<param-name>resteasy.scan</param-name>-->  
   <!--<param-value>true</param-value>-->  
   <!--</context-param>-->  
   <context-param>  
     <param-name>contextConfigLocation</param-name>  
     <param-value>classpath:applicationContext.xml</param-value>  
   </context-param>  
   <listener>  
     <listener-class>  
       org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap  
     </listener-class>  
   </listener>  
   <listener>  
     <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>  
   </listener>  
   <servlet>  
     <servlet-name>resteasy-servlet</servlet-name>  
     <servlet-class>  
       org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher  
     </servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>resteasy-servlet</servlet-name>  
     <url-pattern>/*</url-pattern>  
   </servlet-mapping>  
 </web-app>  


Step 5: Building the Project
mvn clean install

Step 6: Deploying the Service
Now you can deploy this service on to any container. Here we'll deploy this on Jetty using Maven.

mvn jetty:run

This should start a Jetty instance and deploy the application. You can access the HTTP GET service endpoints at the following endpoints.

http://localhost:8080/UserService/get/1 - Get the user with Id = 1
http://localhost:8080/UserService/getAll - List all users

Step 7: Testing the RESTful Web Service using CURL


We're going to test our Web Service using CURL, which I found as a very easy way to send REST Calls. I found this very nice blog about RESTing with Curl.

Installing CURL

sudo apt-get install curl

-I am using Ubuntu, if you're using Win or Mac, please do the necessary to install curl.

Now, after successfully installing CURL, we are ready to test the services.

GET Resources:
- Retrieving all the users.
 curl -v -H "Accept: application/json" http://localhost:8080/UserService/getAll

- Retrieving a user for a given id(id = 1).
 curl -v -H "Accept: application/json" http://localhost:8080/UserService/get/1

POST a Resource: Adding a new user resource.
- Adding the new user record in the add.json file.
 { 
  "user":{ 
    "id":1, 
    "firstName":"First Name1", 
    "lastName":"Last Name1" 
  } 
} 

 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d @add.json http://127.0.0.1:8080/UserService/add


PUT a Resource: Updating an existing user resource.
- Updating an existing user record in the update.json file.
 { 
  "user":{ 
    "id":1, 
    "firstName":"Updated First Name1", 
    "lastName" :"Updated Last Name1" 
  } 
} 

 curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d @update.json http://127.0.0.1:8080/UserService/update/

Summary:
This tutorial looked at how we can create RESTful Web Service using RESTEasy and test the services using CURL.

You can find the sample code here.