Spring boot series

Spring Boot REST API Workflow

Dispatcher servlet spring boot
  • Request is received by DispatcherServlet, which is responsible for processing any incoming URI requests & mapping them to their corresponding handlers in the form of controller methods
  • After the  controller method has been executed, the resource is then processed as a response which can either be JSON or XML
  • 2 processes Execute controller method and Build response entity are implemented be developer and the rest is executed by Spring services

Spring Controller

  • It is a class which is responsible for processing incoming REST API requests, preparing model and returning the view to be rendered as a response
  • It is annotated either by @Controller or @RestController annotation
  • Primary function of both annotations is to allow a class to be recognized as a Spring managed component & to allow handling of HTTP requests using REST API

Annotations

  1. @Controller
  • It is a specialisation of the generic type @Component annotation (it turns the class into a Spring Bean at the auto scan time) which allow a class to be recognized as a Spring managed component
  • It marks the annotated class as a business or presentation layer
  • When a request is made this will inform the DispatcherServlet to include the controller class in scanning for the methods mapped by @RequestMapping (or @GetMapping, @PostMapping, etc)
  1. @Autowired

It is the annotation used to automatically inject dependencies by spring dependency injection of the specified type into the current bean

  1. @GetMapping

Shortcut for @RequestMapping(method=RequestMethod.GET) and used to map HTTP GET requests to the mapped controller methods

  1. @ResponseBody
    • It can be applied at the class level of the controller or can be applied to each response type. 
    • E.g. @GetMapping(“/{id}”)

    public @ResponseBody Tree getTreeById(@PathVariable int id) 

        return repository.findById(id)    

  • When the request handlers return back data it serialise the response to JSON before being returned to the client

5.  @RestController

  • It is just a combination of @Controller and @ResponseBody
  • Added during Spring 4.0 to remove the redundancy of declaring the @ResponseBody annotation 
  • All methods have the @ResponseBody annotations applied to them as @RestController applier it at class-level

6. @Entity

@Entity

public class Student {

    @Id

    @GeneratedValue(strategy=GenerationType.AUTO)

    private Long id;

    private String name;

    // fields, getters and setters

}

  • Entities in JPA are POJOs representing data that can be persisted to the DB
  • It represents a table stored in DB
  • Every instance of an entity represents a row in the table
  • This annotation is specified at the class-level
  • This class must have no-arg constructor and a primary key
  • It must not be declared final 

7. @Id

  • This annotation defines a primary key
  • Ecah JPA entity must have a primary key that uniquely identifies it

8.  @GeneratedValue

  • Specifies different ways to generate IDs
  • We can choose from

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *