TODO
注解 | 作用 |
@Null | 限制只能为null |
@NotNull | 限制必须不为null |
@NotEmpty | 只作用于字符串类型,字符串不为空,并且长度不为0 |
@NotBlank | 只作用于字符串类型,字符串不为空,并且trim()后不为空串 |
@AssertFalse | 限制必须为false |
@AssertTrue | 限制必须为true |
@DecimalMax(value) | 限制必须为一个不大于指定值的数字 |
@DecimalMin(value) | 限制必须为一个不小于指定值的数字 |
@Digits(integer,fraction) | 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction |
@Future | 限制必须是一个将来的日期 |
@Past | 验证注解的元素值(日期类型)比当前时间早 |
@Max(value) | 限制必须为一个不大于指定值的数字 |
@Min(value) | 限制必须为一个不小于指定值的数字 |
@Pattern(value) | 限制必须符合指定的正则表达式 |
@Size(max,min) | 限制字符长度必须在min到max之间 |
验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式 | |
@RestController
@RequestMapping("/tracemain")
public class TraceMainController {
@PostMapping("/save")
public ResponseEntity saveTraceMain(
@NotNull(message = "type字段不能为空") Integer type) {
// xxxx
return ResponseEntity.success(traceMainSaveOutVo);
}
}
@RestController
@RequestMapping("/tracemain")
public class TraceMainController {
@PostMapping("/save")
public ResponseEntity saveTraceMain(@RequestBody @Validated TraceMainSaveInVo traceMainSaveInVo) {
// xxxx
return ResponseEntity.success(traceMainSaveOutVo);
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TraceMainSaveInVo extends SysModel {
private static final long serialVersionUID = -1826896482893289340L;
@ApiModelProperty("物流唯一单号")
@NotBlank(message = "物流唯一单号不能为空")
private String logisticsUniqueNo;
@ApiModelProperty("HQ跟踪单号,格式为:HQ1+19位数字")
@Pattern(regexp = "^HQ1+\\d{19}", message = "HQ跟踪单号格式有误,格式为:HQ1+19位数字")
@Size(min = 22, max = 22, message = "HQ跟踪单号格式有误, 格式为:HQ1+19位数字")
private String hqTraceNo;
@ApiModelProperty("预报来源: 1-WMS,2-LMS, 3-1track")
@NotNull(message = "预报来源不能为空")
@Min(value = 1, message = "预报来源的值只能为1,2,3")
@Max(value = 3, message = "预报来源的值只能为1,2,3")
private Integer predictSource;
}
@RestController
@RequestMapping("/xxx")
@Validated
public class PredictController {
@PostMapping("/save")
public ResponseEntity> saveOrUpdatePredict(@RequestBody @Valid List predictInVoList) {
// xxx
return ResponseEntity.success(predictOutVoList);
}
}
public class PredictInVo extends SysModel {
@NotBlank(message = "OMS订单编号oms_order_no不能为空")
private String omsOrderNo;
}
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = IntegerEnumValidator.class)
public @interface IntegerEnum {
String message();
Class>[] groups() default {};
Class extends Payload>[] payload() default {};
int[] intValues();
}
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.metadata.ConstraintDescriptor;
import java.util.Map;
public class IntegerEnumValidator implements ConstraintValidator {
private final String intVaules = "intValues";
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
if(null == value) {
return true;
}
ConstraintDescriptor> constraintDescriptor = ((ConstraintValidatorContextImpl) context).getConstraintDescriptor();
Map attributes = constraintDescriptor.getAttributes();
int[] intValues = (int[]) attributes.get(intVaules);
boolean hasFlag = false;
for (int intValue : intValues) {
if (intValue == ((int) value)) {
hasFlag = true;
break;
}
}
return hasFlag;
}
}
@SpringBootApplication
public class TssAcquireApplication {
/**
* spring validation校验异常处理
*/
@ControllerAdvice
class TssApiExceptionHandler {
@ExceptionHandler(ConstraintViolationException.class)
public void replayException(ConstraintViolationException replayException) {
Set> constraintViolations = replayException.getConstraintViolations();
if (!CollectionUtils.isEmpty(constraintViolations)) {
StringBuilder errorInfo = new StringBuilder();
constraintViolations.forEach(eachConstraintViolation -> {
errorInfo.append(eachConstraintViolation.getMessageTemplate()).append(";");
});
ResponseEntity
@Valid
@NotNull(message = "货运类型不能为空")
@IntegerEnum(intValues = {0, 1, 2, 3, 4, 5, 6, 7, 9}, message = "货运类型transport_type只能是0,1,2,3,4,5,6,7,9之一")
private Integer transportType;