文章目录
未完待续。。。
一、@RequestParam
用于接受前端提交的参数
1.1 注解参数
返回值 | 参数 | 作用 |
---|---|---|
String | value | name别名 |
String | name | 请求参数对应的属性 |
boolean | required | 是否必传 |
Stirng | defaultValue | 默认值 |
1.2 注解示例
localhost:8080/hello?username=张三
@GetMapping("/hello")
public String func(@RequestParam(name = "name", required = false, defaultValue = "张三") String name) {
return "OK";
}
二、@Pathvarable
用于接受前端url路径参数,通过路径参数作为接口接受的参数,Result风格
2.1 注解参数
返回值 | 参数 | 作用 |
---|---|---|
String | value | name别名 |
String | name | url参数的绑定值 |
2.2 注解示例
localhost:8080/hello/1
@GetMapping("/hello/{id}")
public String func(@PathVariable String id) {
return id;
}
三、@JsonView
用于实现结果字段范围,例如密码字段是不允许返回可以定义接口,根据接口实现返回的字段
3.1 注解参数
返回值 | 参数 | 作用 |
---|---|---|
Class | value | 接口的类名 |
3.2 注解示例
package com.hyt.mtf;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class User {
public interface UserSimpleView { }
public interface UserDetailView extends UserSimpleView { }
private String username;
private String password;
@JsonView(UserSimpleView.class)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@JsonView(UserDetailView.class)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* 根据JsonView视图接口现实指定字段
*/
@GetMapping("/hello/user")
@JsonView({User.UserDetailView.class})
public User func() {
User user = new User("张三", "123456");
return user;
}
四、@RequestMapping
用于springMvc controller控制器,接受前端请求的接口
4.1 注解参数
返回值 | 参数 | 作用 |
---|---|---|
String[] | value | url映射地址别名 |
String[] | path | url映射地址 |
RequestMethod[] | method | 请求方式 |
4.2 注解示例
@RequestMapping(path = {"/hello", "/hello/abc"}, method = RequestMethod.GET)
public String func() {
return "OK";
}
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
组合注解可以代替
五、@Controller @Service @Repository @Configuration @Component
用于标注各个层级的注解,注入到spring容器管理
@Controller @Service @Repository @Configuration 都是@Component组合注解只是区分层级关系
5.1 常用参数
注解 | 层级 |
---|---|
@Controller | 标注于控制层 |
@Service | 标注于业务层 |
@Repository | 标注于持久层 |
@Configuration | 标注于配置层 |
5.2 注解示例
@Controller
public class UserController {
}
六、@RestController
@Controller @ResponseBody组合注解 用于标注该类是个Controller层,并以Json格式返回给前端
6.1 注解示例
@RestController
public class UserController {
}
七、@RequestBody
用于接收前端请求的Json参数,并且封装到对应的JavaBean
7.1 注解示例
@RequestMapping()
public String func(@RequestBody User user) {
return "OK";
}
八、@ResponseBody
用于将返回值以Json格式进行返回,可使用@RestController组合注解代替
8.1 注解示例
@Controller
public class UserController {
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "hello spring";
}
}
九、@Valid 和 BindingResult
用于前端请求参数校验,@Valid需要配合BindingResult使用,配合下面各种校验注解进行使用
以下注解都可以自定义message。看示例
9.1 注解参数
注解 | 作用 |
---|---|
@Valid | 进行校验前端入参 |
注解 | 作用 |
---|---|
@Null | 限制只能为null |
@NotNull | 限制必须不为null |
@AssertFalse | 限制必须为false |
@AssertTrue | 限制必须为true |
@DecimalMax(value) | 限制必须为一个不大于指定值的数字 |
@DecimalMin(value) | 限制必须为一个不小于指定值的数字 |
@Digits(integer,fraction) | 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction |
@Future | 限制必须是一个将来的日期 |
@Max(value) | 限制必须为一个不大于指定值的数字 |
@Min(value) | 限制必须为一个不小于指定值的数字 |
@Past | 限制必须是一个过去的日期 |
@Pattern(value) | 限制必须符合指定的正则表达式 |
@Size(max,min) | 限制字符长度必须在min到max之间 |
@Past | 验证注解的元素值(日期类型)比当前时间早 |
@NotEmpty | 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) |
@NotBlank | 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格 |
验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式 |
9.2 注解示例
/**
* 接受前端的请求并且根据User实体类注解进行校验
*/
@PostMapping("/hello/user")
public String hello(@Valid @RequestBody User user, BindingResult result) {
if (result.hasErrors()) {
result.getAllErrors().forEach(System.out::println);
}
System.out.println(user);
return "hello spring";
}
@Data
public class User {
@NotBlank(message = "用户名不能为空")
private String username;
@NotBlank(message = "密码名不能为空")
private String password;
}
十、@Data
注解在类上, 为类提供读写属性(get方法,set方法)), 此外还提供了 equals()、hashCode()、toString() 方法
基于lombok包
10.1 注解示例
@Data
public class User {
private String username;
private String password;
}
十一、@Slf4j
注解在类上,为类提供一个属性名为 log 的 log4j 的日志对象,避免重复代码
基于lombok包
11.1 注解示例
@Service
@Slf4j
public class UserService {
public void func() {
log.info("【使用{}】","@Slf4j");
}
}
十二、@Synchronized
注解在方法上, 为方法提供同步锁
基于lombok包
12.1 注解示例
@Synchronized
public void func() {
log.info("【使用{}】","@Slf4j");
}
十三、@NoArgsConstructor @AllArgsConstructor
生成无参和有参构造方法注解
基于lombok包
13.1 注解示例
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String username;
private String password;
}