02-Annotation

1. @Deprecated

1. java9

  1. 使用它存在风险,可能导致错误
  2. 可能在未来版本中不兼容
  3. 可能在未来版本中删除
  4. 一个更好和更高效的方案已经取代它

2. @RequestHeader

1. 获取指定的请求头

@PostMapping("/ooxx")
ResultUtil<Boolean> ooxx(@RequestHeader("X-Ticket") String ticket, @RequestBody @Validated Ooxx ooxx);

2. 获取全部请求头

/**
 * 接收省内请求省间同步接口,在省间同步省内数据
 *
 * @param paramList 字符串集合
 * @return
 */
@PostMapping("/headers")
public ResultUtil headers(@RequestHeader Map<String, String> headers, @RequestBody String paramList) {
    log.info("获取请求头参数User-Agent:{}", headers.get("user-agent"));
    return null;
}







 



3. httpServletRequest

private void infoRequestHeaders() {
    Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
        log.info("headerName: {}, => value: {}", headerName, httpServletRequest.getHeader(headerName));
    }
}

 


 


3. @Value

1. 默认值

// String类型不可以有空格
@Value("${autoConfig:autoConfig}")
private String autoConfig;

// boolean类型可以有空格
@Value("${isDel: false}")
private boolean isDel;

 



 

4. @SuppressWarnings

Java魔法堂:注解用法详解——@SuppressWarningsopen in new window

1. unchecked

  • @SuppressWarnings("unchecked")
  • 抑制强转的警告
aUpyV3B5QVE5S0ovM2ZiQVpTdm5STk9ENVJpR2xiTUU1cTNkcHIvUk53PT0=

2. rawtypes

  • @SuppressWarnings("rawtypes")
Ky9EWjd1SXFWRE95aWhlWG9tUk9GZE9ENVJpR2xiTUU1cTNkcHIvUk53PT0=

5. @Validated

1. String

  1. @NotBlank 非空校验(不为null,也不为“”)
  2. @Length 长度校验
  3. @Pattern 正则校验

只有 @Length 没有 @NotBlank 是不可以的。 {"loginName": null} {} 两种情况都会被校验通过

@NotBlank(message = "name为空")
@Length(min = 5, max = 20, message = "名称长度不小于5位,不大于20位")
@Pattern(regexp = "^((?=.*[a-zA-Z])(?=.*[0-9]))[A-Za-z0-9]*$", message = "名称格式不正确")
private String name;

2. byte

1. @Min

  • 大于等于
    1. 《byte》 value != null && value >= min
    2. 《Byte》 value >= min
  • 无默认值传null
    1. 《byte》 value == 0
    2. 《Byte》 value == null
/**
 * type >= 2 && type != null
 */
@Min(value = 2, message = "类型有误")
private byte type = 11;


@NotNull
@Min(value = 2, message = "类型有误")
private Byte type = 11;

6. @Positive

  • 正整数
/**
 * 类型:1-ukey, 2-密码
 */
@Positive(message = "类型有误")
private Byte type = 1;

1. @PositiveOrZero

7. @WebServlet

@WebServlet(name = "AddServlet", urlPatterns = "/add")

8. @Async

@async注解的使用open in new window

9. @ConfigurationProperties

1. 直接返回注入对象

@Resource
private OoxxConfig ooxxConfig;

@RequestMapping("/ooxxConfig")
public OoxxConfig ooxxConfig() {
    return ooxxConfig;
}
{
  "appKey": "app200817260019687291",
  "secret": "6c81c568b03288341411f581546e52c69e977d137cbdbac05834739ed12adee6___",
  "host": "http://ip:port",
  "urls": {
    "logout": "/biz/company/logout",
    "freeze": "/biz/account/freeze",
    "list": "/biz/company/account/list",
    // ...
  },
  "targetSource": {
    "targetBeanName": "scopedTarget.ooxxConfig",
    "targetClass": "com.listao.config.OoxxConfig",
    "beanFactory": {
      "parentBeanFactory": {
        "parentBeanFactory": null,
        "beanClassLoader": {
          "parent": {
            "parent": null,
            "urls": [
              "file:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/sunec.jar",
              "file:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/ext/nashorn.jar",
              // ...
            ]
          },
          "urls": [
            "file:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/charsets.jar",
            "file:/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/jre/lib/deploy.jar",
            // ...
            {
              "timestamp": "2021-04-08T07:55:36.146+0000",
              "status": 200,
              "error": "OK",
              "message": "Type definition error: [simple type, class org.springframework.context.expression.StandardBeanExpressionResolver]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.listao.config.OoxxConfig$$EnhancerBySpringCGLIB$$77b1a5bd[\"targetSource\"]->org.springframework.aop.target.SimpleBeanTargetSource[\"beanFactory\"]->org.springframework.beans.factory.support.DefaultListableBeanFactory[\"parentBeanFactory\"]->org.springframework.beans.factory.support.DefaultListableBeanFactory[\"beanExpressionResolver\"])",
              "path": "/ooxx/ooxxConfig"
            },
@RequestMapping("/ooxxConfig")
public OoxxConfig ooxxConfig() {
    OoxxConfig clone = new OoxxConfig();
    BeanUtil.copyProperties(ooxxConfig, clone, false);
    return clone;
}



 


2. debug

  • debug模式下,显示的注册的属性都为null,可是已经注入了

10. @Transactional

11. @RefreshScope

12. @Pointcut

  • ..:多级包路径匹配
  • *Ctl:匹配类名后缀为Ctl的类
@Pointcut("execution(public * com.listao.controller..*Ctl.*(..))")
public void log() {
}