spring注解4

39 阅读3分钟

1.@ControllerAdvice

官方解释是:It is typically used todefine@ExceptionHandler,

@InitBinder, and@ModelAttribute methods that apply to all@RequestMapping methods

意思是:即把@ControllerAdvice注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。非常简单,不过只有当使用@ExceptionHandler最有用,另外两个用处不大。

@ControllerAdvice  
public class GlobalExceptionHandler {  
    @ExceptionHandler(SQLException.class)  
    @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR,reason=”sql查询错误”)  
    @ResponseBody  
    public ExceptionResponse handleSQLException(HttpServletRequest request, Exception ex) {  
        String message = ex.getMessage();  
        return ExceptionResponse.create(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);  
    } 
}

即表示让Spring捕获到所有抛出的SQLException异常,并交由这个被注解的handleSQLException方法处理,同时使用@ResponseStatus指定了code和reason写到response上,返回给前端。

2.元注解包括 @Retention @Target @Document @Inherited四种

元注解是指注解的注解,比如我们看到的ControllerAdvice注解定义如下。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice {
    XXX
}

@Retention: 定义注解的保留策略:

@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含

@Retention(RetentionPolicy.CLASS) //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,

@Retention(RetentionPolicy.RUNTIME) //注解会在class字节码文件中存在,在运行时可以通过反射获取到

@Target:定义注解的作用目标:

@Target(ElementType.TYPE) //接口、类、枚举、注解

@Target(ElementType.FIELD) //字段、枚举的常量

@Target(ElementType.METHOD) //方法

@Target(ElementType.PARAMETER) //方法参数

@Target(ElementType.CONSTRUCTOR) //构造函数

@Target(ElementType.LOCAL_VARIABLE)//局部变量

@Target(ElementType.ANNOTATION_TYPE)//注解

@Target(ElementType.PACKAGE) ///包

由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等

@Document:说明该注解将被包含在javadoc中

@Inherited:说明子类可以继承父类中的该注解

3.@RequestMapping

处理映射请求的注解。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。有6个属性。

1、 value, method: value:指定请求的实际地址,指定的地址可以是URI Template 模式; method:指定请求的method类型, GET、POST、PUT、DELETE等; 比如:

@RequestMapping(value = "/testValid", method = RequestMethod.POST)
@ResponseBody
public Object testValid(@RequestBody @Valid Test test,BindingResult result, HttpServletRequest request, HttpServletResponse response) {
    XXX
}

value的uri值为以下三类: A) 可以指定为普通的具体值;如@RequestMapping(value ="/testValid") B) 可以指定为含有某变量的一类值;如@RequestMapping(value="/{day}") C) 可以指定为含正则表达式的一类值;如@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\d]+}") 可以匹配../chenyuan122912请求。

2、 consumes,produces: consumes: 指定处理请求的提交内容类型(Content-Type),例如@RequestMapping(value = "/test", consumes="application/json")处理application/json内容类型

produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

3 params、headers: params: 指定request中必须包含某些参数值是,才让该方法处理。

例如:

@RequestMapping(value = "/test", method = RequestMethod.GET, params="name=chenyuan")  
  public void findOrd(String name) {      
    // implementation omitted  
  }

仅处理请求中包含了名为“name”,值为“chenyuan”的请求.

@RequestMapping(value = "/test", method = RequestMethod.GET, headers="Referer=www.baidu.com")  
  public void findOrd(String name) {      
    // implementation omitted  
  }

headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

仅处理request的header中包含了指定“Refer”请求头和对应值为“www.baidu.com”的请求

4. @GetMapping和@PostMapping

@GetMapping(value = "page")等价于@RequestMapping(value = "page", method = RequestMethod.GET)

@PostMapping(value = "page")等价于@RequestMapping(value = "page", method = RequestMethod.POST)