Spring常用注解与接口
# 读取配置信息
读取配置文件application.yml:
port: 9527
library:
books:
- name: a
description: aa
- name: b
description: bb
1
2
3
4
5
6
7
2
3
4
5
6
7
@value
@Value("${port}") String port;
1
2@ConfigurationProperties
读取配置信息并于 bean 绑定
// java.home= @ConfigurationProperties(prefix = "java") public class Bean{ private String name; ... }
1
2
3
4
5
6
# 前后端传值
获取参数
@PathVariable:获取路径参数
@RequestParam:获取查询参数
//请求的 url 是:/{123456}/teachers?type=web @GetMapping("/klasses/{klassId}/teachers") public List<Teacher> getKlassRelatedTeachers( @PathVariable("klassId") Long klassId, @RequestParam(value = "type", required = false) String type){ ... }
1
2
3
4
5
6
7传输数据
请求体中 Content-Type 为 application/json
@PostMapping("/register") public Response registerController(@RequestBody @Valid User user){ registerService.save(user); return Response.ok(); } public class User{ @NotBlank private String userName; @NotBlank private String password; }
1
2
3
4
5
6
7
8
9
10
11
12发送 json数据:
{ "userName":"rui", "password":"123456" }
1
2
3
4
# 注解
# @Import
https://juejin.cn/post/7327137518821736458
# 接口
# ImportBeanDefinitionRegistrar
https://juejin.cn/post/7180731318123118653?searchId=202402282252196299625B65A0D09B886B
# DeferredImportSelector
https://juejin.cn/post/7327138554756464692?searchId=20240228230745DB6AB2EEC09EFAA1B090