Spring provides specialized annotations such as @RestController
, @ResponseBody
and @ResponseStatus
etc. that make RESTful application development easy.
Spring also automatically handles Jackson integration which makes conversion of data from JSON to POJO and vice versa a breeze. Any JSON data received by the controller is automatically converted to a POJO and any POJO returned by the controller is converted to JSON.
REST stands for the REpresentational State Transfer. In the REST architecture, the client and server are implemented independently. REST is also language independent, so the client and server applications can use different programming languages.
The REST architecture is stateless meaning that the client only manages session state and the server only manages the resource state. The communication between the client and server is such that every request contains all the information necessary to interpret it without any previous context.
Both the client and server know the communication format and are able to understand the message sent by the other side. REST calls can be made over HTTP. The client can send HTTP request message to the server where it is processed and an HTTP response is sent back.
The request message has three parts:
- The request line contains the HTTP method (
GET
orPOST
etc.) - The request header contains data with additional information about the request.
- The request body contains the contents of the message.
- The response line contains the status code for success or redirection etc.
- The response header contains additional information about the response like the content type and the size of the response. The client can render the data based on the content type so if it is text/html, it is displayed according to the HTML tags and if it is application/json or application/xml, it is processed accordingly.
- The response body contains the actual message sent in response to the request.