Sobes.tech
Senior

Where is the @RequestHeader annotation placed?

sobes.tech AI

Answer from AI

The @RequestHeader annotation in Spring MVC is used to bind the value of an HTTP header to a method parameter in a controller.

It is directly attached to the method parameter in the controller to obtain the value of a specific header from the incoming HTTP request.

Example:

@GetMapping("/api/data")
public ResponseEntity<String> getData(@RequestHeader("User-Agent") String userAgent) {
    return ResponseEntity.ok("User-Agent: " + userAgent);
}

In this example, the value of the User-Agent header will be passed to the userAgent parameter of the getData method.