|
|
@ -6,10 +6,14 @@ import com.gitee.sop.servercommon.message.ServiceErrorEnum; |
|
|
|
import com.gitee.sop.servercommon.message.ServiceErrorFactory; |
|
|
|
import com.gitee.sop.servercommon.message.ServiceErrorFactory; |
|
|
|
import com.gitee.sop.servercommon.param.validation.ValidationGroupSequence; |
|
|
|
import com.gitee.sop.servercommon.param.validation.ValidationGroupSequence; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
import org.springframework.util.ReflectionUtils; |
|
|
|
|
|
|
|
|
|
|
|
import javax.validation.ConstraintViolation; |
|
|
|
import javax.validation.ConstraintViolation; |
|
|
|
import javax.validation.Validation; |
|
|
|
import javax.validation.Validation; |
|
|
|
import javax.validation.ValidatorFactory; |
|
|
|
import javax.validation.ValidatorFactory; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -23,6 +27,8 @@ public class ServiceParamValidator implements ParamValidator { |
|
|
|
private static final String COMMA = ","; |
|
|
|
private static final String COMMA = ","; |
|
|
|
private static Object[] EMPTY_OBJ_ARRAY = {}; |
|
|
|
private static Object[] EMPTY_OBJ_ARRAY = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final List<String> SYSTEM_PACKAGE_LIST = Arrays.asList("java.lang", "java.math"); |
|
|
|
|
|
|
|
|
|
|
|
private static javax.validation.Validator validator; |
|
|
|
private static javax.validation.Validator validator; |
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
static { |
|
|
@ -35,6 +41,11 @@ public class ServiceParamValidator implements ParamValidator { |
|
|
|
if (obj == null) { |
|
|
|
if (obj == null) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 先校验属性对象
|
|
|
|
|
|
|
|
List<Object> fields = listObjectField(obj); |
|
|
|
|
|
|
|
if (!fields.isEmpty()) { |
|
|
|
|
|
|
|
fields.forEach(this::validateBizParam); |
|
|
|
|
|
|
|
} |
|
|
|
Set<ConstraintViolation<Object>> set = validator.validate(obj, ValidationGroupSequence.class); |
|
|
|
Set<ConstraintViolation<Object>> set = validator.validate(obj, ValidationGroupSequence.class); |
|
|
|
if (!CollectionUtils.isEmpty(set)) { |
|
|
|
if (!CollectionUtils.isEmpty(set)) { |
|
|
|
ConstraintViolation<Object> oneError = set.iterator().next(); |
|
|
|
ConstraintViolation<Object> oneError = set.iterator().next(); |
|
|
@ -43,6 +54,34 @@ public class ServiceParamValidator implements ParamValidator { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<Object> listObjectField(Object object) { |
|
|
|
|
|
|
|
List<Object> ret = new ArrayList<>(); |
|
|
|
|
|
|
|
ReflectionUtils.doWithFields(object.getClass(), field -> { |
|
|
|
|
|
|
|
if (isCustomPackage(field.getType())) { |
|
|
|
|
|
|
|
ReflectionUtils.makeAccessible(field); |
|
|
|
|
|
|
|
ret.add(field.get(object)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 字段类型所在package是否是自定义包 |
|
|
|
|
|
|
|
* @param fieldType 指定的类型 |
|
|
|
|
|
|
|
* @return true,是自定义的 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private boolean isCustomPackage(Class<?> fieldType) { |
|
|
|
|
|
|
|
if (fieldType.isPrimitive()) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Package aPackage = fieldType.getPackage(); |
|
|
|
|
|
|
|
if (aPackage == null) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String packageName = aPackage.getName(); |
|
|
|
|
|
|
|
return !SYSTEM_PACKAGE_LIST.contains(packageName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private RuntimeException getValidateBizParamException(String errorMsg) { |
|
|
|
private RuntimeException getValidateBizParamException(String errorMsg) { |
|
|
|
String subCode = ServiceErrorEnum.ISV_PARAM_ERROR.getErrorMeta().getSubCode(); |
|
|
|
String subCode = ServiceErrorEnum.ISV_PARAM_ERROR.getErrorMeta().getSubCode(); |
|
|
|
String[] msgToken = errorMsg.split(EQ); |
|
|
|
String[] msgToken = errorMsg.split(EQ); |
|
|
|