Spring Boot Key Annotations With Examples

June 26, 2024 ・0comments

 When developing a RESTful service (REST API) using Spring Boot, need to have clear understanding of some of the annotations provided by Spring Boot Web. 


@RestController - RestController is class level annotation used to define a class as a rest full service and it also combines @Controller and @ResponseBody annotations. So the response object is automatically serialised into JSON format.  

@RequestMapping - RequestMapping is used to map web requests to a specific controller or method. For RESTful services, the RequestMapping annotation is typically used at the class level, with HTTP method-specific annotations used at the method level.

@GetMapping, @PostMapping, @PutMapping, @DeleteMapping @PatchMapping- These are specialised versions of the @RequestMapping annotation. they are mostly used at the method level to map HTTP methods. These annotations improve code readability and maintainability as well. 


@PathVariable - PathVariable annotation is used to extract values from URL templates ( ex: hello/{name})

@RequestParam - RequestParam annotation is used to extract query parameters from the URL

@RequestHeader - RequestHeader annotation is used to extract HTTP request headers from an incoming request

@RequestBody -  RequestBody annotation is used to extract the HTTP request body and pass it to the controller method. This can be used with HTTP methods that support a request body (Ex: POST, PUT, Etc)

@ResponseStatus - ResponseStatus annotation is used when we need to pass a different HTTP status other than 200 - OK or map custom exception classes to specific HTTP status codes.

Example Code : 

package com.chandana.helloworld;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/greetings")
public class HelloWorldController {

    //GetMapping. Send GET requests to /api/greetings
    @GetMapping
    public String sayHello() {
        return "Hello, World!";
    }

    //Read PathVariable. Send GET requests to /api/greetings/{name}
    @GetMapping("/{name}")
    public String sayHelloToName(@PathVariable String name) {
        return "Hello, " + name + "!";
    }

    //Read RequestBody. Send POST requests to /api/greetings
    @PostMapping
    public String greetWithPost(@RequestBody String name) {
        return "Greetings, " + name + "!";
    }

    // Read RequestParam. Send GET requests to /api/greetings/query?name=ChandanaNapagoda
    @GetMapping("/query")
    public String greetWithRequestParam(@RequestParam String name) {
        return "Hello there, " + name + "!";
    }

    // Read RequestHeader. Send a request to /api/greetings/header
    @GetMapping("/header")
    public String greetWithHeader(@RequestHeader("X-Name") String name) {
        return "Welcome, " + name + "!";
    }

    // Custom HTTP response status for POST requests to /api/greetings/create
    @PostMapping("/create")
    @ResponseStatus(HttpStatus.CREATED)
    public String createGreeting(@RequestBody String name) {
        return "Created greeting for " + name;
    }
}

Please refrain from copying content to other websites

Post a Comment