@RestController和@Controller

springmvc中,使用@Controller来注明controller层的类属于控制层,在controller层返回的数据形式有以下几种:

页面:静态页面

ModelAndView: 页面+数据

json字符串(数据):如果某个类设计初衷就是返回json字符串,那么该类就可以使用@Controller + @ResponseBody,还可以使@RestCotroller简化书写

@RestController源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any (or empty String otherwise)
     * @since 4.0.1
     */
    @AliasFor(annotation = Controller.class)
    String value() default "";

}

@RestController=@Controller+@ResponseBody

补充:

  在@Controller注解标明的控制层类,可以在某一个方法头部添加@ResponseBody注解  ,不经过视图解析器,只返回json数据(数据)

 参考博客:

  https://www.cnblogs.com/chcha1/p/11274573.html

原文地址:https://www.cnblogs.com/yh-simon/p/12238892.html