控制器注解参数判空
- 调用效果展示
## 正常
{
"id": 14,
"name": "张三",
"sex": "男"
}
## 姓名不能为空
{
"code": -1,
"message": "姓名不能为空",
"data": null
}
## 性别不能为空
{
"code": -1,
"message": "性别不能为空",
"data": null
}
- 代码实现
// 实体类
@Data
@Validated
public class StudentVo {
private Integer id;
@NotNull(message = "姓名不能为空")
private String name;
@NotNull(message = "性别不能为空")
private String sex;
}
// 控制器
@RestController
@RequestMapping("/stu")
public class StudentController {
@PostMapping("/getUser")
public StudentVo studentVo(@Valid @RequestBody StudentVo studentVo) {
StudentVo studentVo1 = new StudentVo();
studentVo1.setId(studentVo.getId());
studentVo1.setName(studentVo.getName());
studentVo1.setSex(studentVo.getSex());
return studentVo1;
}
}
- 引入Jar包
- spring-boot-starter-parent
- lombok