Java – 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.

Best Solution

  • @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

public class MyController { }

public class MyRestController { }