spring 注解系列 控制器层注解 二

 

 

spring 以mvc三层构建了层次分明的javaee开发。

  • @RequestBody 作用于方法上,此注解用于controller类接受URL方法的参数时,表明使用json反序列化将前端传来的参数转化为java对象,故加上该注解则表明前端传参为json字符串。
    @Target({ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface RequestBody {
        boolean required() default true;
    }

     

  • @ResponseBody作用于类和方法上,用于controller类时表明该类的返回参数为json字符串,spring会用默认的Jackson来序列化对象,转化为json字符串返回给前端请求。
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface ResponseBody {
    }
    

     

  • @RequestBody @ResponseBody 两个注解一起用,就表明前后端使用json进行交互。
  • @RestController 作用于类,看源码可知该注解就是 @Controller 和@ResponseBody 两个注解的合体,即可以使用@RestController  注解来代替  @Controller 和@ResponseBody ,简化了注解。
  •  
  • @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Controller
    @ResponseBody
    public @interface RestController {
        String value() default "";
    }
    

     

  • @Autowired 该注解通常用于注解属性,作用是自动装配一个bean 给注解标注的变量。
    @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Autowired {
        boolean required() default true;
    }
  • @Repository 该注解作用于类,用于dao层的注解,扩展了数据库的异常种类,更方便于查找数据库操作时发生异常的处理,value 可以指定bean的标识。
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Repository {
        String value() default "";
    }

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值