Building Spring Microservice
Building Spring Microservice
Embracing the REST philosophy means following these principles in the design and
implementation of web services and APIs. Doing so leads to more scalable, decoupled, and
well-structured systems that can evolve over time and can be accessed by a variety of clients. It
promotes simplicity and uniformity in API design and encourages the use of existing HTTP
standards for communication.
Use URIs to communicate intent. The URIs you use as endpoints for the service should
describe the different resources in your problem domain and provide a basic mechanism for
relationships of resources within it.
Use JSON for your requests and responses. JSON is an extremely lightweight data serialization
protocol, and it’s much easier to consume than XML.
Use HTTP status codes to communicate results. The HTTP protocol has a rich body of standard
response codes to indicate the success or failure of a service. Learn these status codes and,
most importantly, use these consistently across all your services.
@RestController : It is a spring annotation that combines @Controller and @ResponseBody
annotations. It is used to create RESTful web services.
The @Controller annotation marks a class as a controller. A controller is a class that handles
HTTP requests. The @ResponseBody annotation indicates that the return value of the method
will be written directly to the response body. This means that the return value of the method will
not be rendered as a view.
Request Handling: Methods within a class annotated with @RestController are often
annotated with @RequestMapping or other specialized request mapping annotations to specify
the URLs and HTTP methods they can handle.
● value: This attribute specifies the URL path that the handler method will handle.
● method: This attribute specifies the HTTP method that the handler method will handle.
● params: This attribute specifies the HTTP request parameters that the handler method
will handle.
● headers: This attribute specifies the HTTP request headers that the handler method will
handle.
● consumes: This attribute specifies the media types that the handler method can
consume.
● produces: This attribute specifies the media types that the handler method can produce.
The @RequestMapping annotation can be used at the class level or at the method level. When
used at the class level, the annotation specifies the URL path that all handler methods in the
class will handle. When used at the method level, the annotation specifies the URL path and
HTTP method that the handler method will handle.
@PostMapping annotation is a shortcut for the @RequestMapping annotation with the method
attribute set to POST. It is used to map HTTP POST requests to handler methods
The @PostMapping annotation is a convenience annotation that can be used to simplify the
code that is required to map HTTP POST requests to handler methods.
@PutMapping annotation is a shortcut for the @RequestMapping annotation with the method
attribute set to PUT. It is used to map HTTP PUT requests to handler methods
The @PutMapping annotation is a convenience annotation that can be used to simplify the code
that is required to map HTTP PUT requests to handler methods.
The @DeleteMapping annotation is a convenience annotation that can be used to simplify the
code that is required to map HTTP DELETE requests to handler methods.
@PathVariable the @PathVariable annotation is a Spring annotation that is used to extract
values from the URL path and bind them to method parameters.
The @PathVariable annotation can be used with any HTTP method. It is most commonly used
with the @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping annotations.
The @PathVariable annotation is a powerful tool that can be used to create more dynamic and
flexible RESTful web services.
The @RequestParam annotation is a Spring annotation that is used to extract values from the
request parameters and bind them to method parameters.
The @RequestParam annotation can be used with any HTTP method. It is most commonly
used with the @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping
annotations.