Spring注解之@RestController与@Controller的区别
2021-03-06 20:29
标签:value auto ppi nbsp 方法 lan 页面 def map https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html Types that carry this annotation are treated as controllers where NOTE: such as the 翻译一下 一个能代替它自己的比较方便的注解是 @Controller 和 @ResponseBody 携带此注释的类型被视为默认@RequestMapping方法采用@ResponseBody语义的控制器 如果配置了适当的 HandlerMapping-HandlerAdapter 映射与适配器对,类似于MVC中的Java配置和命名空间中的默认值 就像RequestMappingHandlerMapping-RequestMappingHandlerAdapter对,则@RestController会被调用用处理 注意咯,这里指的是,@RestController 等价于 @Controller 和 @ResponseBody https://docs.spring.io/spring-framework/docs/current/javadoc-api/ This annotation serves as a specialization of It is typically used in combination with annotated handler methods based on the 翻译一下 指示带注解的类是“控制器”(例如web控制器)。 此注释是 @Component 的特有一种特有形式,允许实现类通过路径扫描被自动检测到 它通常用于结合加注解的基于RequestMapping注解的的处理方法 @RestController无法返回指定页面,而@Controller可以。 对于Controller, 如果只是使用@RestController注解,则其方法无法返回指定页面 此时配置的视图解析器 InternalResourceViewResolver 不起作用,返回的内容就是 return 里的内容。 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行 @RestController 可以返回JSON格式字符串 Spring注解之@RestController与@Controller的区别 标签:value auto ppi nbsp 方法 lan 页面 def map 原文地址:https://www.cnblogs.com/YC-L/p/14288180.html @RestController官方地址
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController
@Controller
and @ResponseBody
.
@RequestMapping
methods assume @ResponseBody
semantics by default.@RestController
is processed if an appropriate HandlerMapping
-HandlerAdapter
pair is configuredRequestMappingHandlerMapping
-RequestMappingHandlerAdapter
pair which are the default in the MVC Java config and the MVC namespace. @Controller官方地址
@Target(value=TYPE)
@Retention(value=RUNTIME)
@Documented
@Component
public @interface Controller
@Component
, allowing for implementation classes to be autodetected through classpath scanning.RequestMapping
annotation. 二者区别
下一篇:java 注解
文章标题:Spring注解之@RestController与@Controller的区别
文章链接:http://soscw.com/essay/61021.html