Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring Basics Creating Controllers and Views Create a Controller to Handle HTTP Requests

Thomas Salai
Thomas Salai
4,964 Points

why do I need @ResponseBody

Why do I need @ResponseBody on my Controller method

public String listGifs(){ return "List of all the Gifs"; }

1 Answer

Chris Ramacciotti
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Chris Ramacciotti
Treehouse Guest Teacher

Hi Thomas! @ResponseBody means that the controller's return value should be used as the body of the response. That is, the HTTP response should be whatever is stated in the return value. Without the annotation, Spring would try to use the configured template engine (Thymeleaf, unless you configured otherwise) to render a template named "List of all the Gifs". This is why using a browser to visit the URI listed in your @RequestMapping annotation yields a plain page with the text "List of all the Gifs". There is no template rendering happening here, like you'll see (or have now seen) later in the course. I show you this annotation in advance of talking about template rendering, so if you have yet to move on, you'll see that we end up removing the annotation.

Nice!