Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program.
What is meant by @PostMapping annotation?
@PostMapping annotation in Spring MVC framework is a powerful tool for handling the HTTP POST requests in your RESTful web services. It maps specific URLs to handler methods allowing you to receive and process data submitted through POST requests. The @PostMapping annotation is a Spring annotation that is used to map HTTP POST requests onto specific handler methods. It is a shortcut for @RequestMapping annotation with method = RequestMethod.POST attribute.
Examples of @PostMapping annotation
Example 1:
Java
// on the below line we are adding a post mapping annotation // and passing the end point to it on which // we have to make a post request. @PostMapping ( "/addStudent" ) public Student addStudent( @RequestBody String studentName) { return "Hello, " +studentName; } |
In the above example, addStudent method is annotated with @PostMapping annotation. This means that addStudent method will be called when an HTTP Post request is received at the /addStudent URL. Inside the addStudent method, we are also passing a parameter for Request Body to be sent while making a post request. In the request body, we have to pass the student name which is to be inserted.
Example 2:
Java
@PostMapping ( "/students/{id}" ) public void updateStudent( @PathVariable ( "id" ) String id, @RequestBody Student student) { // Update the student record here. } |
In the above example updateStudent method is annotated with @PostMapping annotation. This means that updateStudent method will be called when an HTTP Post request is received at the /students/{id} URL. Inside this updateStudent method we are also passing a parameter for Request Body to be send while making a post request. In the request body we have to pass the id for which we have to update the student details and the object for student which will contain the updated values of the student.
What is meant by @GetMapping annotation?
@GetMapping annotation in Spring is a powerful for building RESTful web services. It maps HTTP GET requests to a specific handler method in Spring controllers. With the help of @GetMapping annotation we can easily define endpoints of RESTful API and handle various HTTP requests. @GetMapping annotation is used for mapping HTTP GET requests onto specific handler methods. It is composed annotation that acts as a shortcut for @RequestMapping(method=RequestMethod.GET).
Examples of @GetMapping annotation
Example 1:
Java
@GetMapping ( "/students" ) public List<Student> getAllStudents() { return studentRepository.findAll(); } |
In the above example getAllStudents method is annotated with @GetMapping annotation. This means that getAllStudents method will be called when an HTTP Get request is received at the /students URL.
Example 2:
Java
@GetMapping ( "/students/{rollNo}" ) public Student getStudent( @PathVariable ( "rollNo" ) String rollNo) { // in this method we can return // the student which we get from the api call. } |
In the above example getStudent method is annotated with @GetMapping annotation. This means that getStudent method will be called when an HTTP Get request is received at the /students/{rollNo} URL. Inside this url we have to specify the roll number for the student which we have to fetch.