user3104319 user3104319 - 4 months ago 52
Java Question

Difference between spring @Controller and @RestController annotation

Difference between spring

@Controller
and
@RestController
annotation.

Can
@Controller
annotation be used for both Web MVC and REST applications?

If yes, how can we differentiate if it is Web MVC or REST application.

Answer
  • @Controller is used to mark classes as Spring MVC Controller.
  • @RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations (see: Javadoc)

So the following two controller definitions should do the same

@Controller
@ResponseBody
public class MyController { }

@RestController
public class MyRestController { }
Comments