Spring is one of the most widely used Java EE framework. Once the response object is returned by the handler method, Mapping Jackson2Http Message Converter kicks in and convert it to JSON [email protected] Variable annotation is the easy way to extract the data from the rest URI and map it to the method [email protected] Body annotation is used to map the request body JSON data into the Employee object, again this is done by the Mapping Jackson2Http Message Converter mapping.

dating spring tx-25

ID="+emp Id); return emp Data.get(emp Id); } @Request Mapping(value = Emp Rest URIConstants. GET) public @Response Body List emp Id Keys = emp Set(); for(Integer i : emp Id Keys){ emps.add(emp Data.get(i)); } return emps; } @Request Mapping(value = Emp Rest URIConstants. POST) public @Response Body Employee create Employee(@Request Body Employee emp) { logger.info("Start create Employee."); Created Date(new Date()); emp Data.put(Id(), emp); return emp; } @Request Mapping(value = Emp Rest URIConstants. PUT) public @Response Body Employee delete Employee(@Path Variable("id") int emp Id) { logger.info("Start delete Employee."); Employee emp = emp Data.get(emp Id); emp Data.remove(emp Id); return emp; } } For simplicity, I am storing all the employees data in the Hash Map emp Data.

*/ @Controller public class Employee Controller { private static final Logger logger = Logger Logger(Employee Controller.class); //Map to store employees, ideally we should use database Map(); @Request Mapping(value = Emp Rest URIConstants. GET) public @Response Body Employee get Dummy Employee() { logger.info("Start get Dummy Employee"); Employee emp = new Employee(); Id(9999); Name("Dummy"); Created Date(new Date()); emp Data.put(9999, emp); return emp; } @Request Mapping(value = Emp Rest URIConstants. GET) public @Response Body Employee get Employee(@Path Variable("id") int emp Id) { logger.info("Start get Employee.

Employee; /** * Handles requests for the Employee service.

By having this configuration, we will be using JSON in request body and we will receive JSON data in the response. Date; import com.fasterxml.jackson.databind.annotation. We can use Spring package com.journaldev.spring; import GET_ALL_EMP, List.class); println(emps.size()); for(Linked Hash Map map : emps){ println("ID="+map.get("id")+", Name="+map.get("name")+", Created Date="+map.get("created Date"));; } } private static void test Create Employee() { Rest Template rest Template = new Rest Template(); Employee emp = new Employee(); Id(1);Name("Pankaj Kumar"); Employee response = rest For Object(SERVER_URI+Emp Rest URIConstants.

Let’s write a simple POJO class that will serve as input and output to our Restful web service methods. CREATE_EMP, emp, Employee.class); print Emp Data(response); } private static void test Get Employee() { Rest Template rest Template = new Rest Template(); Employee emp = rest For Object(SERVER_URI+"/rest/emp/1", Employee.class); print Emp Data(emp); } private static void test Get Dummy Employee() { Rest Template rest Template = new Rest Template(); Employee emp = rest For Object(SERVER_URI+Emp Rest URIConstants.

We can also specify the HTTP method that should be used by client application to invoke the rest [email protected] Body annotation is used to map the response object in the response body.

@Request Mapping annotation is used to map the request URI to the handler method.

The tutorial is developed in Spring STS IDE for creating Spring MVC skeleton code easily and then extended to implement Restful architecture. I am using Wiz Tools Rest Client to invoke the rest calls but you can also use Chrome extension Postman.

STS tool generates the file for us, however I have updated the Spring Framework, Aspect J, SLF4J and Jackson version to the latest one as of today. Below screenshots shows the different invocations of the rest apis exposed by our application and it’s output.