Spring @RestController, @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping Annotation Example


This page will walk through Spring @RestController, @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping Annotation Example.  To handle the HTTP request in the application Spring Framework provides these annotations, some of annotated at the class level and some of at method level.

Related Post: Spring MVC @Controller, @RequestMapping, @RequestParam, and @PathVariable Annotation Example

1. @RestController Annotation

@Restontroller annotation was introduced in Spring version 4. It is a convenience annotation that is itself annotated with @Controller and @ResponseBody.

A class annotated with @RestController annotation.

RestControllerDemo.java
package org.websparrow;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestControllerDemo {

	@RequestMapping("/home")
	public String hello() {
		return "Welcome to Websparrow";
	}
}

A class without using @RestController annotation.

ControllerDemo.java
package org.websparrow;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ControllerDemo {

	@RequestMapping("/home")
	@ResponseBody
	public String hello() {
		return "Welcome to Websparrow";
	}
}

Both above classes return the “Welcome to Websparrow” as output and if you do not add @ResponseBody annotation in ControllerDemo class, it will throw the exception.

@RestController = @Controller + @ResponseBody

2. @GetMapping Annotation

@GetMapping annotation is handled HTTP GET request and it is used at method level only. It was introduced in 4.3 version.

GetMappingDemo.java
package org.websparrow;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GetMappingDemo {

	@GetMapping(value = "/home")
	public String hello() {
		return "Welcome to Websparrow";
	}
}

@GetMapping = @RequestMapping(value=”/home”, method = RequestMethod.GET)

3. @PostMapping Annotation

@PostMapping annotation is handled HTTP POST request. @PostMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.POST). It was also introduced in Spring 4.3 version.

@PostMapping = @RequestMapping(value=”/save”, method = RequestMethod.POST)

PostMappingDemo.java
package org.websparrow;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.websparrow.entity.Country;

@RestController
public class PostMappingDemo {

	@PostMapping(value = "/save")
	public Country save(@RequestBody Country country) {
        // TODO: save logic
		return country;
	}
}

4. @PutMapping Annotation

@PutMapping annotation is used for mapping HTTP PUT requests onto specific handler methods. If you want to update existing data use @PutMapping annotation.

@PutMapping = @RequestMapping(value=”/update/{id}”, method = RequestMethod.PUT)

PutMappingDemo.java
package org.websparrow;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.websparrow.entity.Country;

@RestController
public class PutMappingDemo {

	@PutMapping(value = "/update/{id}")
	public String update(@PathVariable("id") int countryId, @RequestBody Country country) {
        // TODO: update logic 
		return "Country updated successfully";
	}
}

5. @DeleteMapping Annotation

@DeleteMapping annotation is handled HTTP DELETE request.

DeleteMappingDemo.java
package org.websparrow;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DeleteMappingDemo {

	@DeleteMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") int countryId) {
		// TODO: delete logic goes here
		
		return "Country delete from database.";
	}
}

@DeleteMapping = @RequestMapping(value=”/delete/{id}”, method = RequestMethod.DELETE)

References

  1. Spring MVC @Controller, @RequestMapping, @RequestParam, and @PathVariable Annotation Example
  2. @RestController
  3. @GetMapping
  4. @PostMapping
  5. @PutMapping
  6. @DeleteMapping

Similar Posts

About the Author

Websparrow
I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.