Classes annotated as @Controller
in Spring are responsible for handling incoming web requests. When annotating methods or the entire
controller with @ResponseBody
, the return value of said methods will be serialized and set as the response body. In other words, it tells
the Spring framework that this method does not produce a view. This mechanism is commonly used to create API endpoints.
Spring provides @RestController
as a convenient annotation to replace the combination of @Controller
and
@ResponseBody
. The two are functionally identical, so the single annotation approach is preferred.
This rule will raise an issue on a class that is annotated with @Controller
if:
- the class is also annotated with
@ResponseBody
or
- all methods in said class are annotated with
@ResponseBody
.