0x56794E
0x56794E

Reputation: 21281

Why do I need to add @ResponseBody to my controller action?

According to Spring documentation, this annotation indicates that a method return value should be bound to the web response body. I understand that, and I've been using this for my ajax calls. However, I recently came across code that doesn't use the annotation.

So I guess my question really is why it works without the annotation?

Upvotes: 0

Views: 1039

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

Without the annotation, a different process takes place. Depending on the return type (you can find the defaults in this document) the response will be generated differently.

For example, if your return type is String, then, by default, the return value will be resolved as a View name, a ViewResolver will try to resolve and create a View object, and a RequestDispatcher will forward/include/redirect to it (ex. a jsp) so that the Servlet container can handle generating the response.

The actual interface that handles the return type is HandlerMethodReturnValueHandler and there are many implementations for each type. See here for more information.

Upvotes: 3

Related Questions