加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

@Controller和@RestController源码解析

发布时间:2020-12-14 06:32:32 所属栏目:Java 来源:网络整理
导读:2018年不知不觉已经走到了尾声,你还在为分不清 @Controller和@Restcontroller而烦恼吗?这篇博文从源码层面分析这两个注解,值得一读。 首先贴一张源码的图,对比一下,左边是@Controller的源码,右边是@RestController的。 如果觉得不清楚,看下面代码: @
@Override 表示当前的方法定义将覆盖超类中的方法 @Deprecated 如果程序中使用了注解为它的元素,那么编译器会发出警告 @SuppressWarnings 关闭不当的编译器警告信息 ?
元注解是专门负责注解其他的注解,Java内置了四种元注解,专门负责新注解的创建,直接看这张表格(摘自Java编程思想):
表示该注解可以用于什么地方。可能的ElementType参数包括:
CONSTRUCTOR:构造器的声明
FIELD:域声明(包括enum实例)
LOCAL_VARIABLE:局部变量声明
METHOD:方法声明
PACKAGE:包声明
PARAMETER:参数声明
TYPE:类、接口(包括注解类型)和enum声明
表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括:
SOURCE:注解将在编译器丢弃
CLASS:注解在class文件中可用,但会被VM丢弃
RUNTIME:VM将在运行期也保留注解,因此可以通过反射机制读取注解的信息
现在,明白了@Target({ElementType.TYPE})、@Retention(RetentionPolicy.RUNTIME)、@Documented的作用,我们也可以自定义一个注解@Algorithms:

@ ""

@Component注解

@Component注解源码如下:

@ </span><span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt; * The value may indicate a suggestion for a logical component name,if any (or empty String otherwise) </span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt; String value() </span><span style="color: #0000ff;"&gt;default</span> ""<span style="color: #000000;"&gt;;

}

加了@Component注解,表明这是一个逻辑组件,告知Spring要为它创建bean。相当于xml配置文件中的

@AliasFor注解

@AliasFor注解是一个用于声明注解属性别名的注解,源码如下:

@ </span><span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt; * Alias for {</span><span style="color: #808080;"&gt;@link</span><span style="color: #008000;"&gt; #attribute}. * <p>Intended to be used instead of {</span><span style="color: #808080;"&gt;@link</span><span style="color: #008000;"&gt; #attribute} when {</span><span style="color: #808080;"&gt;@link</span><span style="color: #008000;"&gt; #annotation} * is not declared &amp;mdash; for example: {</span><span style="color: #808080;"&gt;@code</span><span style="color: #008000;"&gt; @AliasFor("value")} instead of * {</span><span style="color: #808080;"&gt;@code</span><span style="color: #008000;"&gt; @AliasFor(attribute = "value")}. </span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt; @AliasFor(</span>"attribute"<span style="color: #000000;"&gt;) String value() </span><span style="color: #0000ff;"&gt;default</span> ""<span style="color: #000000;"&gt;; </span><span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt; * The name of the attribute that <em>this</em> attribute is an alias for. * </span><span style="color: #808080;"&gt;@see</span><span style="color: #008000;"&gt; #value </span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt; @AliasFor(</span>"value"<span style="color: #000000;"&gt;) String attribute() </span><span style="color: #0000ff;"&gt;default</span> ""<span style="color: #000000;"&gt;; </span><span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt; * The type of annotation in which the aliased {</span><span style="color: #808080;"&gt;@link</span><span style="color: #008000;"&gt; #attribute} is declared. * <p>Defaults to {</span><span style="color: #808080;"&gt;@link</span><span style="color: #008000;"&gt; Annotation},implying that the aliased attribute is * declared in the same annotation as <em>this</em> attribute. </span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt; Class</span><? <span style="color: #0000ff;"&gt;extends</span> Annotation> annotation() <span style="color: #0000ff;"&gt;default</span> Annotation.<span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt;;

}

@Controller中的@AliasFor(annotation = Component.class)说明@Controller是Component的一个别名,本质上还是一个Component,正如注释中所说“to be turned into a Spring bean in case of an??autodetected component.”,可以被扫描成一个bean。

同理,@RestController中的@AliasFor(annotation = Controller.class)说明@RestController是Controller的一个别名,是一个Controller,再本质一点说,是个Component,是个Spring bean。

@ResponseBody注解

@}

提到@ResponseBody注解,就不得不提一个名词:表述性状态转移(Representational State Transfer,REST)。

什么是表述性状态转移呢?拆开来看:

表述性:REST资源实际上可以用各种形式来表述,包括JSON、XML、HTML等;

状态:使用TEST的时候,我们更关注资源的状态而不是对资源采取的行为;

转移:以某种形式(例如JSON)从一个应用转换到另一个应用,例如从服务器到客户端。

简单讲,REST就是将资源的状态以最适合客户端或者服务器的形式从服务器转移到客户端(或者反过来)。

在Spring 4.0版本中,Spring支持借助@ResponseBody注解和各种HttpMethodConverter,替换基于视图的渲染方式,实现对REST的支持。当然Spring对REST的支持远不止这一种方式。

@ResponseBody注解告知Spring,要将返回的对象作为资源发送给客户端。这个过程跳过正常的模型/视图流程中视图解析的过程,而是使用Spring自带的各种Http消息转换器将控制器产生的数据转换为客户端需要的表述形式。如果客户端请求头的Accept表明他能接受“application/json”,并且Jackson JSON在类路径下面,那么处理方法返回的对象将交给MappingJacksonHTTPMessageConverter,并由他转换为JSON返回给客户端;如果客户端想要“text/html”格式,那么Jaxb2RootElementHttpMessageConverter将会为客户端产生XML响应。

当处理请求时候,@ResponseBody和@RequestBody是启用消息转换的一种简介和强大方式。但是,如果控制器里面的每个方法都需要信息转换功能的话,那么这些注解就会带有一定程度的重复性。

所以,Spring 4.0引入了@RestController注解,如果在控制器类上面使用@RestController注解,我们不必再为每个方法添加@ResponseBody注解,因为Spring会为该控制器下面的所有方法应用消息转换功能。这也是这个Controller之所以叫Controller的原因,正所谓见名知意。

总结

@RestController相当于@ResponseBody + @Controller一起作用。

如果控制器产生的结果希望让人看到,那么它产生的模型数据需要渲染到视图中,从而可以展示到浏览器中,使用@Controller。

如果控制器产生的结果不需要让人看到,那么它产生的数据经过消息转换器直接返回到浏览器,使用@RestController。

参考文献:

[1] Bruce?Eckel.?Java编程思想(第四版)[M].?陈昊鹏译.?北京:机械工业出版社,2007.

[2] Craig Walls. Spring实战(第4版)[M]. 张卫滨译.?北京:人民邮电出版社,2016.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

2018年不知不觉已经走到了尾声,你还在为分不清@Controller和@Restcontroller而烦恼吗?这篇博文从源码层面分析这两个注解,值得一读。

首先贴一张源码的图,对比一下,左边是@Controller的源码,右边是@RestController的。

如果觉得不清楚,看下面代码:

@Controller:

@= Component. ""

@RestController:

@ </span><span style="color: #008000;"&gt;/**</span><span style="color: #008000;"&gt; * The value may indicate a suggestion for a logical component name,if any (or empty String otherwise) * </span><span style="color: #808080;"&gt;@since</span><span style="color: #008000;"&gt; 4.0.1 </span><span style="color: #008000;"&gt;*/</span><span style="color: #000000;"&gt; @AliasFor(annotation </span>= Controller.<span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt;) String value() </span><span style="color: #0000ff;"&gt;default</span> ""<span style="color: #000000;"&gt;;

}

显然,两个注解的源码里面都包含许多的注解:

@Controller的注解包括:@Target({ElementType.TYPE})、@Retention(RetentionPolicy.RUNTIME)、@Documented、@Component

@RestController的注解包括:@Target(ElementType.TYPE)、@Retention(RetentionPolicy.RUNTIME)、@Documented、@Controller、@ResponseBody

所以,源码的分析也就是对注解的分析。

内置注解和元注解

注解(也被称为元数据)为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便地使用这些数据。

Java SE5中有三种内置注解

    推荐文章
      热点阅读