liveBook · Manning

@responsebody

As explained in Spring Start Here, Second Edition

The @ResponseBody annotation declares that a controller action returns data directly in the HTTP response rather than a view name.

@ResponseBody tells Spring MVC to send a controller action’s return value directly in the HTTP response, rather than treating it as a view name. section 10.2 section 10.6

Listing 10.1 Implementing a REST endpoint action in a controller class
@Controller    #A
public class HelloController {
 
  @GetMapping("/hello")   #B
  @ResponseBody              #C
  public String hello() {
    return "Hello!";
  }
}

Here, the /hello action returns “Hello!” as the response body. section 10.2

For controllers with several REST actions, repeating the annotation can be avoided by using @RestController, which combines @Controller and @ResponseBody. section 10.2

How Spring Start Here, Second Edition covers it

How deep it goes
Explains direct response-body behavior, contrasts it with view-name handling, and demonstrates a simple endpoint.
Examples
Uses a /hello action that returns “Hello!” and contrasts repeated method annotations with @RestController.
What you can do after
Apply it in Spring MVC to return an action’s data directly in the HTTP response.
How it connects
Connects the annotation with Spring MVC, @Controller, @RestController, and REST endpoints.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.