Introduction to REST

REST stands for Representational State Transfer. REST is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services.

Representational state transfer (REST) -

https://en.m.wikipedia.org/wiki/Representational_state_transfer

"Web resources" were first defined on the World Wide Web as documents or files identified by their URLs. However, today they have a much more generic and abstract definition that encompasses everything or entity that can be identified, named, addressed, or handled, in any way whatsoever, on the Web. In a RESTful Web service, requests made to a resource's URI will elicit a response with a payload formatted in HTML, XML, JSON, or some other format. The response can confirm that some alteration has been made to the stored resource, and the response can provide hypertext links to other related resources or collections of resources. When HTTP is used, as is most common, the operations (HTTP methods) available are GET, POST, PUT, PATCH, and DELETE.

• To Create a resource : HTTP POST should be used

• To Retrieve a resource : HTTP GET should be used

• To Update a resource : HTTP PUT should be used

• To Delete a resource : HTTP DELETE should be used

REST API in Java Using Spring

Detailed Explanation

@RestController : Spring RestController annotation is a convenience annotation that is itself annotated with @Controller and @ResponseBody . This annotation is applied to a class to mark it as a request handler. Spring RestController annotation is used to create RESTful web services using Spring MVC.

Example:

@RestController

public class StudentRESTAPIController{

/ * * Your Logic /

}

@RquestMapping :RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods. @RequestMapping annotation on methods tells Spring to use this method as handler for any client requests. This is very important spring annotation, we can configure additional features such as HTTP methods supported, Content-Type of request, response content type etc. By default, GET method is supported and JSON response is returned.

Example:

@RestController

@RequestMapping(“/api”)

public class StudentRESTAPIController{

@RequestMapping(value = "/student/", method = RequestMethod.GET)

public ResponseEntity> listAllStudents()

{

/* *Your Logic */

}

}

@RequestBody : The @RequestBody annotation binds the HTTPRequest body to the domain object. Spring framework automatically deserializes incoming HTTPRequest to the Java object using Http Message Converters.

@ResponseBody : @ResponseBody is a Spring annotation which binds a method return value to the web response body. It is not interpreted as a view name. It uses HTTP Message converters to convert the return value to HTTP response body, based on the content-type in the request HTTP header.

ResponseEntity : ResponseEntity represents an HTTP response, including headers, body, and status. While @ResponseBody puts the return value into the body of the response, ResponseEntity also allows us to add headers and status code.

@PathVariable : @PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable. If the method parameter is Map then the map is populated with all path variable names and values. It has the following optional elements: name - name of the path variable to bind to.

@RequestParam : @RequestParam is a Spring annotation used to bind a web request parameter to a method parameter. It has the following optional elements: defaultValue - used as a fallback when the request parameter is not provided or has an empty value. name - name of the request parameter to bind to.

Basically, @RestController , @RequestBody, ResponseEntity & @PathVariable are all you need to know to implement a REST API in Spring. Spring Framework provides several classes to help you implement rest apis in java. The Spring framework supports two ways of creating RESTful services:

• using MVC with ModelAndView

• using HTTP message converters

The new approach, based on HttpMessageConverter and annotations, is much more lightweight and easy to implement. Configuration is minimal, and it provides sensible defaults for what you would expect from a RESTful service.

Thanks for reading blog. Stay Connected with Us. In Next Blog We will do Practical Examples using Spring Boot and STS i.e Spring Tool Suite.

This is Open Community Blogger Platform and community forum of SFDC professionals interested to share their experiences, ideas, and thoughts or any content invaluable to help others in this domain.

Archives