@Controller与@RestController

发布时间 2023-09-14 09:05:18作者: yub4by

https://blog.csdn.net/qq_31016939/article/details/131363158
https://blog.csdn.net/moshowgame/article/details/82869151
https://blog.csdn.net/u013154103/article/details/79783884

  1. @Controller与@RestController的区别都是用来表示Spring某个类是否可以接收Http请求。

  2. @Controller用来标识一个Spring类是SpringMVC controller处理器,@Controller类中的方法可以直接通过返回string跳转jsp、ftl、html等模板页面。

  3. @Controller类中在方法上加上@ResponseBody注解,也可以返回实体对象。

  4. @RestController是@Controller和@ReponseBody的结合体,两个标注合起来的作用。@RestController类中的所有方法只能返回string、object、json等实体对象,不能跳转到模板页面。

  5. @RestContoller类中相当于所有方法都自带@ResponseBody,会自动将方法的返回值转换为json格式的响应体返回给客户端。@RestController如果想跳转页面,可以使用ModelAndView进行封装。

	@GetMapping("/xxx")
    public ModelAndView active(){
		//xxxxxx
        return new ModelAndView("页面名称(不含后缀)");
}
@RestController
public class LoginController {

    @GetMapping("/authentication/require")
    public ModelAndView require() {
        return new ModelAndView("ftl/login");
    }

}