Repaso
EspaƱol

Which annotation reads a JSON request body into a Java object?

Options

  1. @PathVariable
  2. @RequestParam
  3. @RequestBody (Correct option)
  4. @ResponseBody

Answer

@RequestBody

Why

@RequestBody tells Spring to read the HTTP body (usually JSON) and convert it into the Java parameter with Jackson.

@RequestParam reads query parameters (/search?name=ana), @PathVariable reads parts of the path.

@ResponseBody is the opposite direction (object to body); @RestController already applies it to all methods.

@PostMapping
public ResponseEntity<User> create(@Valid @RequestBody CreateUserRequest req) {
    User created = userService.create(req);
    return ResponseEntity.status(HttpStatus.CREATED).body(created);   // 201
}

Remember

Body = @RequestBody. Query = @RequestParam. Path = @PathVariable.

Key terms

Practice this question in Repaso