代码配置
controller
import javax. validation. Valid;
import java. util. List;
import java. util. Map;
@Slf4j
@RestController
public class VenderInfoController {
@PostMapping ( value = "/assets/venders" )
public VenderInfoVo addVenderInfoData ( @RequestBody @Valid VenderInfoVo venderInfoVo) {
log. info ( "add vender info data :{}" , venderInfoVo. toString ( ) ) ;
return null;
}
@PutMapping ( value = "/assets/venders" , consumes = { MediaType. APPLICATION_FORM_URLENCODED_VALUE} )
public VenderInfoVo updateVenderInfoData ( @RequestParam Map< String, String> map) throws Exception {
log. info ( "update vender info data :{}" , map. toString ( ) ) ;
VenderInfoParam venderInfoParam = new VenderInfoParam ( ) ;
venderInfoParam. setId ( map. getOrDefault ( "id" , "" ) ) ;
venderInfoParam. setName ( map. getOrDefault ( "name" , "" ) ) ;
venderInfoParam. setRemarks ( map. getOrDefault ( "remarks" , "" ) ) ;
VenderInfoVo venderInfoVo = VenderInfoParam. buildVenderInfoVo ( venderInfoParam) ;
ValidatorUtils. validateFast ( venderInfoVo) ;
return null;
}
}
vo
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AssetInfoVo implements Serializable {
@Valid
private String id;
@Valid
@NotNull
@Size ( min = 12 , max = 12 , message = "设备编码长度应为12" )
@Pattern ( regexp = "[0-9]+" , message = "设备编码只能包含数字" )
private String deviceNumber;
@Valid
@NotNull
@Size ( max = 64 , message = "{digitalSn}{size}{validation.max}" )
private String digitalSn;
@Valid
@NotNull
@Size ( max = 32 , message = "{version}{size}{validation.max}" )
private String version;
}
验证util
package com. qax. pylon. assets. commons. utils;
import com. qax. pylon. commons. biz. po. ErrorDetail;
import com. qax. pylon. commons. constant. HttpStatus;
import com. qax. pylon. commons. exception. BaseException;
import org. hibernate. validator. HibernateValidator;
import javax. validation. ConstraintViolation;
import javax. validation. Validation;
import javax. validation. Validator;
import java. util. Iterator;
import java. util. Set;
public class ValidatorUtils {
private static Validator validatorFast = Validation. byProvider ( HibernateValidator. class ) . configure ( ) . failFast ( true ) . buildValidatorFactory ( ) . getValidator ( ) ;
private static Validator validatorAll = Validation. byProvider ( HibernateValidator. class ) . configure ( ) . failFast ( false ) . buildValidatorFactory ( ) . getValidator ( ) ;
public static < T> Set< ConstraintViolation< T> > validateFast ( T domain) throws Exception {
Set< ConstraintViolation< T> > validateResult = validatorFast. validate ( domain) ;
if ( validateResult. size ( ) > 0 ) {
throw new BaseException (
HttpStatus. INTERNAL,
ErrorDetail. builder ( ) . field ( validateResult. iterator ( ) . next ( ) . getPropertyPath ( ) . toString ( ) ) . description ( validateResult. iterator ( ) . next ( ) . getMessage ( ) ) . build ( ) ) ;
}
return validateResult;
}
public static < T> Set< ConstraintViolation< T> > validateAll ( T domain) throws Exception {
Set< ConstraintViolation< T> > validateResult = validatorAll. validate ( domain) ;
if ( validateResult. size ( ) > 0 ) {
Iterator< ConstraintViolation< T> > it = validateResult. iterator ( ) ;
while ( it. hasNext ( ) ) {
ConstraintViolation< T> cv = it. next ( ) ;
System. out. println ( cv. getPropertyPath ( ) + ":" + cv. getMessage ( ) ) ;
}
}
return validateResult;
}
}