让代码变得简洁,总结一二

1、使用Optional.ofNullable校验入参非空

   Optional.ofNullable(user).orElseThrow(() -> new DefineException("获取当前登录用户失败。"));

2、使用@RequiredArgsConstructor去掉臃肿的注解(Controller去掉@Autowired/@Resource)

(1)在Controller类名上增加注解:@RequiredArgsConstructor

(2)在变量前增加:private final

3、入参注解校验

******接口中,入参类型前增加注解:@Validated 

@NotBlank一般用来校验String类型不能为空

@NotBlank(message = "")

@NotNull一般用来校验Integer类型不能为空

@NotEmpty一般用来校验List类型不能为空



posted @ 2023-01-06 13:29  sun-sailing  阅读(13)  评论(0编辑  收藏  举报