What is the purpose of the @RequestBody annotation?

@RequestBody binds the raw body of an incoming HTTP request to a Java method parameter, automatically converting JSON (or XML) into a corresponding object using an HttpMessageConverter.

Key Points: • Spring uses Jackson by default to deserialize JSON into the annotated parameter's type. • It's typically combined with @Valid to trigger bean validation on the deserialized object. • Only one @RequestBody parameter is allowed per method, since the request body can only be read once. • Contrasts with @RequestParam/@PathVariable, which read from the query string or URL path rather than the body. • If the incoming JSON doesn't match the target class's structure, Spring returns a 400 Bad Request with a deserialization error by default.

Example: A POST request with JSON body {"name":"Alice","email":"alice@example.com"} sent to a createUser endpoint is automatically converted into a populated UserDto object, ready to use in the method without any manual parsing.

Code Example:

@PostMapping("/users")
public UserDto createUser(@Valid @RequestBody UserDto dto) {
    return userService.create(dto);
}

Interview Tip: A concise interview answer is:

"@RequestBody tells Spring to deserialize the incoming request body -- usually JSON -- straight into a Java object using Jackson, so I can work with a typed DTO instead of parsing raw text. I almost always pair it with @Valid to get bean validation applied automatically on the way in."