0% found this document useful (0 votes)
5 views

Building Spring Microservice

Uploaded by

Aditi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Building Spring Microservice

Uploaded by

Aditi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

In general, we can use the following guidelines for implementing service interface design:

Stateless Communication: RESTful services are designed to be stateless, meaning that


each request from a client to a server must contain all the information needed to
understand and process the request. There should be no session state stored on the
server between requests. Stateless design simplifies scalability and fault tolerance.
Resource-Based Architecture: REST treats everything as a resource. Resources are
identified by URIs (Uniform Resource Identifiers). Each resource should have a unique
URI that is used to access, manipulate, and represent it. Resources are manipulated
using standard HTTP methods like GET, POST, PUT, DELETE, etc.
HTTP Methods: RESTful APIs make effective use of HTTP methods, such as GET for
retrieving resources, POST for creating new resources, PUT for updating existing
resources, and DELETE for removing resources. Each HTTP method corresponds to a
specific action on the resource.
Representation: Resources are represented in a format such as XML, JSON, or HTML.
Clients request a representation of the resource, and the server responds with the
resource's current state in the requested format. This allows clients to understand and
process the data.
HATEOAS (Hypermedia as the Engine of Application State): HATEOAS is an optional,
advanced constraint of REST. It means that the response should contain hypermedia
links that guide the client on how to navigate the application. Clients should be able to
discover available actions dynamically rather than relying on out-of-band information.

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.

The @RestController annotation is a convenience annotation that combines the @Controller


and @ResponseBody annotations. This means that you can use the @RestController
annotation to create RESTful web services without having to annotate every method with the
@ResponseBody annotation.

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.

Response Serialization: By default, the data returned by methods in a @RestController class


is automatically serialized into JSON format (or other formats like XML) using Spring's built-in
message converters.

@RequestMapping annotation is a Spring annotation that is used to map HTTP requests to


handler methods. Handler methods are methods in a controller that handle HTTP requests.

The @RequestMapping annotation has the following attributes:

● 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.

@GetMapping: In the Spring Framework, the @GetMapping annotation is used to map a


specific HTTP GET request to a handler method in a controller class. It's a type of request
mapping annotation that helps define how incoming HTTP requests should be handled and
which method should be invoked to process those requests.

@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.

@DeleteMapping annotation is a shortcut for the @RequestMapping annotation with the


method attribute set to DELETE. It is used to map HTTP DELETE 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 has the following attributes:

● value: This attribute specifies the name of the path variable.


● required: This attribute specifies whether the path variable is required. If the path
variable is not required, and the value is not present in the URL path, the method will still
be executed, but the path variable parameter will be null.

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.

The @RequestParam annotation has the following attributes:


● value: This attribute specifies the name of the request parameter.
● required: This attribute specifies whether the request parameter is required. If the
request parameter is not required, and the value is not present in the request
parameters, the method will still be executed, but the request parameter parameter will
be null.
● defaultValue: This attribute specifies the default value of the request parameter. If the
request parameter is not present in the request parameters, the default value will be
used.

ResponseEntity is a class in the Spring Framework used to represent an HTTP response,


including the status code, headers, and body. It provides a flexible way to customize the entire
HTTP response

Key Features of ResponseEntity:


Status Code: Allows setting of HTTP status codes (e.g., 200 OK, 404 Not Found, 500 Internal
Server Error).
Headers: Supports setting custom HTTP headers.
Body: Can include a response body, which can be any type of data (e.g., String, JSON object,
file).

Common Use Cases:


● Returning custom HTTP status codes.
● Adding custom headers to the response.
● Returning complex response bodies, such as JSON or XML data.

You might also like