自定义注解部分
@Target ( ElementType. FIELD)
@Retention ( RetentionPolicy. RUNTIME)
@Constraint ( validatedBy = OvertimePlaceConvertValidator. class)
public @interface OvertimePlaceConvert {
int overtimeLocation ( ) default 0 ;
Class< ? > [ ] groups ( ) default { } ;
Class< ? extends Payload> [ ] payload ( ) default { } ;
}
自定义注解逻辑处理类部分
public class OvertimePlaceConvertValidator implements ConstraintValidator< OvertimePlaceConvert, Object> {
private Integer overtimeLocation;
@Override
public void initialize ( OvertimePlaceConvert constraintAnnotation) {
overtimeLocation = constraintAnnotation. overtimeLocation ( ) ;
ConstraintValidator. super. initialize ( constraintAnnotation) ;
}
@Override
public boolean isValid ( Object o, ConstraintValidatorContext constraintValidatorContext) {
if ( o == null) {
return true;
}
if ( o instanceof Integer) {
String overtimePlaceConvert = OvertimePlaceConvert ( overtimeLocation) ;
if ( overtimePlaceConvert != null) {
return true;
}
}
return false;
}
public String OvertimePlaceConvert ( Integer number) {
String overtimeLocation = null;
if ( number. doubleValue ( ) == 0 ) {
overtimeLocation = "公司" ;
} else if ( number. doubleValue ( ) == 1 ) {
overtimeLocation = "家里" ;
} else if ( number. doubleValue ( ) == 2 ) {
overtimeLocation = "客户现场" ;
} else if ( number. doubleValue ( ) == 3 ) {
overtimeLocation = "其他" ;
}
return overtimeLocation;
}
}