clean code之优雅校验

clean code之优雅校验

参数的非空判断,长度判断,集合的非空等等,

接口层校验:demo


public interface TaskService {
    /**
     * insert tb_task_record
     *
     * @param taskRecordVo
     * @return
     */
    Integer insertTbTaskRecord(@Valid TaskRecordVo taskRecordVo);
}


@Data
public class TaskRecordVo implements Serializable {
    private static final long serialVersionUID = 9110133117222913758L;

    /**
     * 主键id
     */
    @NotNull(message = "id不能为空")
    private BigInteger id;
    }



@Service
@Validated
public class TaskServiceImpl implements TaskService {

    @Override
    public Integer insertTbTaskRecord(TaskRecordVo taskRecordVo) {
        return 1;
    }

 @Test
    public void testQueryTaskRecordByTaskId() {
        try {
            taskService.insertTbTaskRecord(new TaskRecordVo());
        }catch (Exception e){
            log.info(e.getMessage());
        }
    }

在这里插入图片描述
ConstraintV
在这里插入图片描述

2. 控制层的参数校验demo:


@RestController
@RequestMapping("test")
public class TestValidController {


    @PostMapping(value = "test1")
    public void test(@Valid @RequestBody Param Param) {
        Preconditions.checkArgument(StringUtils.isNotBlank(Param.getId()), "id不能为空");
        Preconditions.checkArgument(CollectionUtils.isNotEmpty(Param.getList()), "List不能为空");
        //do something
    }


    @Data
    public static class Param {
        @NotNull(message = "不能为空")
        private String id;

        @NotEmpty(message = "不能为空")
        private List<String> list;

    }

}

在这里插入图片描述

备注:

接口层的参数校验需要在实现类上加@Validated
控制层只需在参数上加@Valid

接口层抛出的异常是:ConstraintViolationException
控制层抛出的异常是:MethodArgumentNotValidException

参数校验的注解有:
spring-boot已经集成了spring validation和hibernate,直接使用就好了,spring支持的参数注解如下:
@NotNull validates that the annotated property value is not null.
@AssertTrue validates that the annotated property value is true.
@Size validates that the annotated property value has a size between the attributes min and max; can be applied to String, Collection, Map, and array properties.
@Min validates that the annotated property has a value no smaller than the value attribute.
@Max validates that the annotated property has a value no larger than the value attribute.
@Email validates that the annotated property is a valid email address.
Some annotations accept additional attributes, but the message attribute is common to all of them. This is the message that will usually be rendered when the value of the respective property fails validation.
And some additional annotations that can be found in the JSR:
@NotEmpty validates that the property is not null or empty; can be applied to String, Collection, Map or Array values.
@NotBlank can be applied only to text values and validates that the property is not null or whitespace.
@Positive and @PositiveOrZero apply to numeric values and validate that they are strictly positive, or positive including 0.
@Negative and @NegativeOrZero apply to numeric values and validate that they are strictly negative, or negative including 0.
@Past and @PastOrPresent validate that a date value is in the past or the past including the present; can be applied to date types including those added in Java 8.
@Future and @FutureOrPresent validate that a date value is in the future, or in the future including the present.