Request Controller
Overview
As part of his doctoral work, Roy Fielding generalized the web’s architectural principles and presented them as a framework of constraints or an architectural style. Fielding referred to this architectural style as Representational State Transfer or REST.
Rest is the most commonly used integration style for application-to-application communication.
What is REST?
Rest Stands for Representational State Transfer.
REST is nothing but an architectural style around the web. It encourages us to use the web with some constraints and guiding principles below.
- Everything is a resource.
- Each resource is identified by a unique identifier (URI).
- Use the Standard HTTP Method.
- Resources can have multiple representations.
- Be stateless.
It provides standards between computer systems on the web, making it easier for systems to communicate with each other.
How to Do It?
Spring boot as a framework simplifies the development of restful web services without writing boilerplate code. Let's understand how to expose restful web services using spring boot.
Add Required Dependency
The only dependency we need is the following :
With this one dependency, our application is web-enabled and ready to expose the endpoint. This is without the need for manual configuration of the dispatcher servlet, content negotiator, serializer, deserializer, etc.
Add Controller
To handle the web request, we need to have a controller added to the application. Controllers are special beans in the spring framework ecosystem capable of handling HTTP requests. Spring boot provides a special annotation @RestController to mark the class as a controller.
@RestController Annotation
This "annotation" is another example of meta-annotation or convenience annotation like @SpringBootApplication. The @RestController annotation comprises two annotations.
- @Controller :
It’s a Spring stereotype annotation similar to @Bean and @Repository and declares the annotated class as an MVC Controller. - @ResponseBody :
A Spring annotation binds a method return value to the web response body. It uses HTTP Message converters to convert the return value to the HTTP response body based on the content type in the request HTTP header.
Expose REST Endpoint
There are four basic HTTP verbs we use in requests to interact with resources in a REST system, and spring boot provides the corresponding annotation for each of those.
Http Verb | Spring Annotation | Description |
---|---|---|
GET | @GetMapping | Retrieve a resource |
POST | @PostMapping | Create a resource |
PUT | @PutMapping | Modify a resource |
DELETE | @DeleteMapping | Delete a resource |
These annotations contain parameters that deal with the HTTP request and response. A few key parameters are
-
Path :
An URL path or resource location. For example path for all the employees will be /employees -
Headers :
The headers of the mapped request, narrowing the primary mapping. Header attributes restrict the scope of the controller method. Request only mapped if each such title is found to have the given value.For example :
will match requests with a Content-Type of "text/html".
-
Consumes :
Narrows the primary mapping by media types that the handler can consume. For example, if your API supports only JSON format as input. It can be restricted using @GetMapping(value = "/employee", consumes="application/json"). It's an array type, meaning you can supply more than one value. -
Produces :
Narrows the primary mapping by media types that the mapped handler can produce.
Let's apply this knowledge and expose a couple of endpoints.
The @RequestMapping(/employee) on the class applies the mapping the all the handlers. This is convenient as we don't have to repeat the same mapping in all the handler methods. We have two APIs.
- /employee :
Return all the employees. - /employee/{id} :
Return employee with specific id supplied in the path variable.
Response of the above APIs will be :
Conclusions
- REST is a trendy integration style for service-to-service communication.
- REST is an architectural style.
- Spring boot provides annotation @RestController to mark the class as controller and capable of handling web requests.
- Spring boot provides mapping annotation for each type of HTTP verb.