mirror of https://github.com/halo-dev/halo
Refactor ValidationUtils
parent
cd9313c929
commit
ac4d50c785
|
@ -1,56 +1,85 @@
|
||||||
package cc.ryanc.halo.utils;
|
package cc.ryanc.halo.utils;
|
||||||
|
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.validation.FieldError;
|
import org.springframework.validation.FieldError;
|
||||||
|
|
||||||
import javax.validation.ConstraintViolation;
|
import javax.validation.ConstraintViolation;
|
||||||
import java.util.HashMap;
|
import javax.validation.ConstraintViolationException;
|
||||||
import java.util.List;
|
import javax.validation.Validation;
|
||||||
import java.util.Map;
|
import javax.validation.Validator;
|
||||||
import java.util.Set;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 有关字段验证的工具类
|
* Object validation utilities.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
|
* @data 03/29/19
|
||||||
*/
|
*/
|
||||||
public class ValidationUtils {
|
public class ValidationUtils {
|
||||||
|
|
||||||
|
private static Validator VALIDATOR;
|
||||||
|
|
||||||
private ValidationUtils() {
|
private ValidationUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets validator, or create it.
|
||||||
|
*
|
||||||
|
* @return validator
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
public static Validator getValidatorOrCreate() {
|
||||||
|
if (VALIDATOR == null) {
|
||||||
|
synchronized (ValidationUtils.class) {
|
||||||
|
// Init the validation
|
||||||
|
VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return VALIDATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates bean by hand.
|
||||||
|
*
|
||||||
|
* @param obj bean to be validated
|
||||||
|
* @param groups validation group
|
||||||
|
* @throws ConstraintViolationException throw if validation failure
|
||||||
|
*/
|
||||||
|
public static void validate(Object obj, Class<?>... groups) {
|
||||||
|
|
||||||
|
Validator validator = getValidatorOrCreate();
|
||||||
|
|
||||||
|
// Validate the object
|
||||||
|
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(obj, groups);
|
||||||
|
|
||||||
|
if (!CollectionUtils.isEmpty(constraintViolations)) {
|
||||||
|
// If contain some errors then throw constraint violation exception
|
||||||
|
throw new ConstraintViolationException(constraintViolations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将字段验证错误转换为标准的map型,key:value = field:message
|
* 将字段验证错误转换为标准的map型,key:value = field:message
|
||||||
*
|
*
|
||||||
* @param constraintViolations constraint violations(contain error information)
|
* @param constraintViolations constraint violations(contain error information)
|
||||||
* @return 如果返回null则未出现错误
|
* @return error detail map
|
||||||
*/
|
*/
|
||||||
public static Map<String, String> mapWithValidError(Set<ConstraintViolation<Object>> constraintViolations) {
|
@NonNull
|
||||||
Map<String, String> errMap = null;
|
public static Map<String, String> mapWithValidError(Set<ConstraintViolation<?>> constraintViolations) {
|
||||||
if (!CollectionUtils.isEmpty(constraintViolations)) {
|
if (CollectionUtils.isEmpty(constraintViolations)) {
|
||||||
// if not empty
|
return Collections.emptyMap();
|
||||||
errMap = new HashMap<>(4);
|
|
||||||
for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
|
|
||||||
errMap.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return errMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String stringWithValidError(Set<ConstraintViolation<?>> violations, boolean allError) {
|
Map<String, String> errMap = new HashMap<>(4);
|
||||||
StringBuilder errString = new StringBuilder();
|
// Format the error message
|
||||||
if (!CollectionUtils.isEmpty(violations)) {
|
constraintViolations.forEach(
|
||||||
for (ConstraintViolation<?> violation : violations) {
|
constraintViolation ->
|
||||||
if (errString.length() > 0) {
|
errMap.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage()));
|
||||||
errString.append(",");
|
return errMap;
|
||||||
}
|
|
||||||
errString.append(violation.getMessage());
|
|
||||||
if (!allError && errString.length() > 0) {
|
|
||||||
return errString.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errString.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -59,35 +88,13 @@ public class ValidationUtils {
|
||||||
* @param fieldErrors 字段错误组
|
* @param fieldErrors 字段错误组
|
||||||
* @return 如果返回null,则表示未出现错误
|
* @return 如果返回null,则表示未出现错误
|
||||||
*/
|
*/
|
||||||
public static Map<String, String> mapWithFieldError(List<FieldError> fieldErrors) {
|
public static Map<String, String> mapWithFieldError(@Nullable List<FieldError> fieldErrors) {
|
||||||
Map<String, String> errMap = null;
|
if (CollectionUtils.isEmpty(fieldErrors)) {
|
||||||
|
return Collections.emptyMap();
|
||||||
if (!CollectionUtils.isEmpty(fieldErrors)) {
|
|
||||||
// 如果不为空
|
|
||||||
errMap = new HashMap<>(4);
|
|
||||||
for (FieldError fieldError : fieldErrors) {
|
|
||||||
errMap.put(fieldError.getField(), fieldError.getDefaultMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, String> errMap = new HashMap<>(4);
|
||||||
|
fieldErrors.forEach(filedError -> errMap.put(filedError.getField(), filedError.getDefaultMessage()));
|
||||||
return errMap;
|
return errMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String stringWithFieldError(List<FieldError> fieldErrors, boolean allError) {
|
|
||||||
StringBuilder errString = new StringBuilder();
|
|
||||||
if (!CollectionUtils.isEmpty(fieldErrors)) {
|
|
||||||
// 如果不为空
|
|
||||||
for (FieldError fieldError : fieldErrors) {
|
|
||||||
if (errString.length() > 0) {
|
|
||||||
errString.append(",");
|
|
||||||
}
|
|
||||||
errString.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage());
|
|
||||||
if (!allError && errString.length() > 0) {
|
|
||||||
return errString.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return errString.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -18,10 +18,8 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||||
|
|
||||||
import javax.validation.ConstraintViolation;
|
|
||||||
import javax.validation.ConstraintViolationException;
|
import javax.validation.ConstraintViolationException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exception handler of controller.
|
* Exception handler of controller.
|
||||||
|
@ -54,10 +52,10 @@ public class ControllerExceptionHandler {
|
||||||
@ExceptionHandler(ConstraintViolationException.class)
|
@ExceptionHandler(ConstraintViolationException.class)
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
public BaseResponse handleConstraintViolationException(ConstraintViolationException e) {
|
public BaseResponse handleConstraintViolationException(ConstraintViolationException e) {
|
||||||
BaseResponse<Set<ConstraintViolation<?>>> baseResponse = handleBaseException(e);
|
BaseResponse<Map<String, String>> baseResponse = handleBaseException(e);
|
||||||
baseResponse.setStatus(HttpStatus.BAD_REQUEST.value());
|
baseResponse.setStatus(HttpStatus.BAD_REQUEST.value());
|
||||||
baseResponse.setMessage("Field validation error");
|
baseResponse.setMessage("Field validation error");
|
||||||
baseResponse.setData(e.getConstraintViolations());
|
baseResponse.setData(ValidationUtils.mapWithValidError(e.getConstraintViolations()));
|
||||||
return baseResponse;
|
return baseResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue