Differences and application scenarios between @Controller and @RestController

Differences and application scenarios between @Controller and @RestController

Official document:
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
Meaning:
@RestController annotation is equivalent to @ResponseBody + @Controller combined.

It can also be seen from the source code after the annotation @RestController is opened.
write picture description here

Application scenarios

1) If you just use @RestController to annotate the Controller, the methods in the Controller cannot return to the jsp page, the configured view resolver InternalResourceViewResolver does not work, and the returned content is the content in Return.

For example : If it should have been to the success.jsp page, it will display success.

2) If you need to return to the specified page, you need to use @Controller with the view resolver InternalResourceViewResolver.

3) If you need to return JSON, XML or custom mediaType content to the page, you need to add the @ResponseBody annotation to the corresponding method.

In the development process, the most common interaction with the front-end is json string. It is recommended to use the @RestController annotation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326235127&siteId=291194637