# spring boot注解

# @AutoConfigureBefore 和 @AutoConfigureAfter

  1. @AutoConfigureBefore(AAAA.class) 或 AutoConfigureBefore({AAAA.class, BBBB.class})

    @AutoConfigureBefore(AAAA.class)
    public class CCCC {
    }
    
    1
    2
    3

    CCCC将会在AAAA类之前加载

  2. @AutoConfigureAfter(AAAA.class) 或 AutoConfigureAfter({AAAA.class, BBBB.class})

    @AutoConfigureAfter(AAAA.class)
    public class CCCC {
    }
    
    1
    2
    3

    说明 CCCC 将会在 AAAA 之后加载

# 常用参数注解

常用的用于接收参数的注解有:

  • @PathVariable 搭配rest风格使用
  • RequestHeader 获取请求头信息
  • RequestParam 获取参数信息(?name=chuchen....)
  • CookieValue 获取cookie信息
  • @RequestBody 获取请求体信息

# @PathVariable

@GetMapping("/car/{id}/owner/{username}")

@PathVariable("id") int id,
@PathVariable("username") String username,
@PathVariable Map<String,String> pvs,
1
2
3

能够获取到id和username值,这里如果方法参数是一个Map<String,String>,那么能够将所有的值,都放入Map集合中

请求测试

http://localhost:8080/car/13/owner/chuchen

# RequestHeader

能够获取到请求头的信息

@RequestHeader(value = "User-Agent") String User_Agent,
@RequestHeader Map<String,String> mapHeader,
1
2

如果参数是一个Map<String,String>,同样会将所有的请求头信息放入map中

# RequestParam

获取请求参数

http://localhost:8080/car/13/owner/chuchen?age=34&interest=coding&interest=com&interest=video

@RequestParam("age") int age,
@RequestParam Map<String,String> paramMap,
@RequestParam List<String> interest
1
2
3

结果

但是这里,interest有多个值,我们如果使用Map<String,String>接收所有的参数,那么此map中,只会存入一个interest,并且是第一个

# @CookieValue

@CookieValue("Webstorm-2fb3dfc7") String Webstorm_2fb3dfc7,
@CookieValue("Webstorm-2fb3dfc7")Cookie cookie
1
2

结果

@CookieValue("Webstorm-2fb3dfc7")Cookie cookie会将Webstorm-2fb3dfc7的cookie赋值给Cookie对象,通过这个对象可以获取到所有Webstorm-2fb3dfc7的cookie信息

# @RequestBody可以获取请求体信息

但是此请求必须是post请求,因为只有post才有请求体

@RequestBody String content
1

http://localhost:8080/car/13/owner/chuchen?age=34&interest=coding&interest=com&interest=video

返回

age=34&interest=coding&interest=com&interest=video

# @RequestAttribute

从注解可以获取到request中的setAttribute()的值

@GetMapping("/goto")
public String gotoGet(HttpServletRequest request) {
    request.setAttribute("msg","chuchen");
    request.setAttribute("code",200);
    return "forward:/success";
}

@ResponseBody
@GetMapping("/success")
public Map gotoGet(
    HttpServletRequest request,
    @RequestAttribute("msg") String msg,
    @RequestAttribute("code") Integer code
) {
    Map<String,Object> map = new HashMap<>();
    map.put("msg",msg);
    map.put("code",code);
    return map;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

运行结果

# @MatrixVariable矩阵变量

矩阵变量的使用主要是在,当用户禁用cookie之后,通过矩阵变量的方式获取cookie

其参数传递像下面这样

http://localhost:8080/cars/sell;low=34;brand=byd,audi,yd

使用;进行分割开,如果一个参数,对应着多个值的话,那么可以使用,or:,如上面的brand,可以写成

brand=byd,audi,yd
brand=byd:audi:yd
1
2
// /cars/sell;low=34;brand=byd,audi,yd
@GetMapping("/cars/{path}")
public Map carsSell(@MatrixVariable("low") Integer low,
                    @MatrixVariable("brand") List<String> brand,
                    @PathVariable("path") String path){
    Map<String,Object> map = new HashMap<>();

    map.put("low",low);
    map.put("brand",brand);
    map.put("path",path);
    return map;
}
1
2
3
4
5
6
7
8
9
10
11
12

运行结果

@GetMapping("/cars/{path}")不能直接写成@GetMapping("/cars/sell")这样的话,会获取不到的,必须使用{名称}

在默认情况下,springboot是禁用矩阵变量的,也就是禁用分号;功能,如果没有将removeSemicolonContent设置为false,那么访问的时候,就会报错

org.springframework.web.bind.MissingMatrixVariableException: Required matrix variable 'low' for method parameter type Integer is not present
1

WebMvcAutoConfiguration类中的方法中,可以看到这个设置

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    if (this.mvcProperties.getPathmatch()
        .getMatchingStrategy() == WebMvcProperties.MatchingStrategy.PATH_PATTERN_PARSER) {
        configurer.setPatternParser(new PathPatternParser());
    }
    configurer.setUseSuffixPatternMatch(this.mvcProperties.getPathmatch().isUseSuffixPattern());
    configurer.setUseRegisteredSuffixPatternMatch(
        this.mvcProperties.getPathmatch().isUseRegisteredSuffixPattern());
    this.dispatcherServletPath.ifAvailable((dispatcherPath) -> {
        String servletUrlMapping = dispatcherPath.getServletUrlMapping();
        if (servletUrlMapping.equals("/") && singleDispatcherServlet()) {
            UrlPathHelper urlPathHelper = new UrlPathHelper();
            urlPathHelper.setAlwaysUseFullPath(true);
            configurer.setUrlPathHelper(urlPathHelper);
        }
    });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

UrlPathHelper urlPathHelper = new UrlPathHelper()此对象中,有一个setRemoveSemicolonContent(boolean removeSemicolonContent)方法,就是设置这个分号功能,将此值设置为false就可以了

设置这个,涉及到之前将的springmvc定制功能,可以使用两种方式

第一种

@Bean
public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void configurePathMatch (PathMatchConfigurer configurer) {
            UrlPathHelper helper = new UrlPathHelper();
            helper.setRemoveSemicolonContent(false);
            configurer.setUrlPathHelper(helper);
        }
    };
}
1
2
3
4
5
6
7
8
9
10
11

第二种方式就是让此配置文件,实现WebMvcConfigurer接口,并且重写configurePathMatch (PathMatchConfigurer configurer)方法

@Override
public void configurePathMatch (PathMatchConfigurer configurer) {
    UrlPathHelper helper = new UrlPathHelper();
    helper.setRemoveSemicolonContent(false);
    configurer.setUrlPathHelper(helper);
}
1
2
3
4
5
6

通过这两种方法中的其中一种,便可以解决这个问题

# 参数重复问题

但是如果参数重复,像这样的url/boss/1;age=20/2;age=10,那在获取的时候,就需要指定哪个得到哪个值

@GetMapping("/boss/{bossId}/{empId}")
public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
    Map<String,Object> map = new HashMap<>();

    map.put("bossAge",bossAge);
    map.put("empAge",empAge);
    return map;
}
1
2
3
4
5
6
7
8
9

# ConditionalOnProperty

@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true")
1

此注解就是和配置文件绑定,name就是配置文件中的k,havingValue就是,只有当配置文件中的spring.datasource.druid.stat-view-servlet.enabled = havingValue时,才会加载的

spring.datasource.druid.stat-view-servlet.enabled = true # 只有这样才会加载
1