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 RestControllerannotation is used to create RESTful web services using Spring MVC.Example:
@RestController
public class StudentRESTAPIController{
/
*
* Your Logic
/
}
Example:
@RestController
@RequestMapping(“/api”)
public class StudentRESTAPIController{
@RequestMapping(value = "/student/", method = RequestMethod.GET)
public ResponseEntity> listAllStudents(){
/*
*Your Logic
*/
}
}
@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 String,String 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
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.