import com.hsq.common.enums.ResponseState; import com.hsq.common.response.ResponseVO; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Component; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @Aspect @Component @EnableAspectJAutoProxy(exposeProxy=true) public class ValidAop { /** * 校验传入实体 * * @param pjp * @throws Throwable */ @Around("@annotation(com.hsq.common.validate.Valid)") public Object aroundExec(ProceedingJoinPoint pjp) throws Throwable { MethodSignature ms = (MethodSignature) pjp.getSignature(); Method method = ms.getMethod(); Valid annotation = method.getAnnotation(Valid.class); if (annotation != null) { List errorList=new ArrayList(); Object[] args=pjp.getArgs(); Class>[] checkToken = annotation.value(); Class>[] group = annotation.group(); for (Class c : checkToken) { for (Object obj : args) { if (c == obj.getClass()) { List result=null; if(group.length<1){ result=ValidationUtil.validate(obj); }else{ result=ValidationUtil.validate(obj,group); } if(result!=null) errorList.addAll(result); } } } if(errorList.size()>0){ ResponseVO vo=new ResponseVO(); vo.setResponseCode(ResponseState.CHECKERROR.getCode()); vo.setResponseMsg(ResponseState.CHECKERROR.getMsg()); vo.setData(errorList); return vo; } } return pjp.proceed(); } }
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import java.util.ArrayList; import java.util.List; import java.util.Set; public class ValidationUtil { private static ValidationUtil util; private Validator validator; public ValidationUtil() { // TODO Auto-generated constructor stub ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); this.validator = factory.getValidator(); } /** * @return the validator */ public Validator getValidator() { return validator; } public static ValidationUtil getValidationUtil() { if (util == null) { util = new ValidationUtil(); } return util; } public static List validate(Object o) { Set> set = ValidationUtil .getValidationUtil().getValidator().validate(o); List errorList = null; HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); for (ConstraintViolation cv : set) { if (errorList == null) { errorList = new ArrayList(); } String message = MessageResolver.getMessage(request, cv.getMessage()); errorList.add(new CheckErrorResultBean(cv.getPropertyPath() .toString(), message)); } return errorList; } /** * 验证实体列表的正确性 * @param objList * @return */ public static List validateList(List objList) { if (null == objList || objList.isEmpty()) { return null; } List strList = new ArrayList(); for (Object obj : objList) { List checkErrorResultBean = validate(obj); if(null != checkErrorResultBean){ strList.addAll(validate(obj)); } } return strList; } public static List validate(Object o,Class> ...c) { Set> set = ValidationUtil .getValidationUtil().getValidator().validate(o,c); List errorList = null; HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); for (ConstraintViolation cv : set) { if (errorList == null) { errorList = new ArrayList(); } String message = MessageResolver.getMessage(request, cv.getMessage()); errorList.add(new CheckErrorResultBean(cv.getPropertyPath() .toString(), message)); } return errorList; } }
import org.apache.commons.lang3.StringUtils; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.support.RequestContextUtils; import javax.servlet.http.HttpServletRequest; import java.util.Locale; public class MessageResolver { /** * 获得国际化信息 * * @param request * HttpServletRequest * @param code * 国际化代码 * 替换参数 * @return * @see org.springframework.context.MessageSource#getMessage(String, * Object[], Locale) */ public static String getMessage(HttpServletRequest request, String code) { WebApplicationContext messageSource = RequestContextUtils.findWebApplicationContext(request); if (messageSource == null) { throw new IllegalStateException("WebApplicationContext not found!"); } LocaleResolver localeResolver = RequestContextUtils .getLocaleResolver(request); Locale locale; if (localeResolver != null) { locale = localeResolver.resolveLocale(request); } else { locale = request.getLocale(); } if(locale==null|| StringUtils.isEmpty(locale.getLanguage())||locale.getLanguage().equalsIgnoreCase("null")){ locale=Locale.CHINA; } return messageSource.getMessage(code.trim(), null,code,locale); } }
import com.paic.sealCenter.common.enums.ResponseState; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel public class ResponseVO { @ApiModelProperty(value = "响应码", dataType = "string") private String responseCode; @ApiModelProperty(value = "响应消息", dataType = "string") private String responseMsg; @ApiModelProperty(value = "响应数据") private T data; public ResponseVO() { this.setResponseCode(ResponseState.SUCCESS.getCode()); this.setResponseMsg(ResponseState.SUCCESS.getMsg()); } public ResponseVO(String responseCode) { this.setResponseCode(responseCode); } public ResponseVO(String responseCode, String responseMsg) { this.setResponseCode(responseCode); this.setResponseMsg(responseMsg); } public String getResponseCode() { return responseCode; } public ResponseVO setResponseCode(String responseCode) { this.responseCode = responseCode; return this; } public String getResponseMsg() { return responseMsg; } public ResponseVO setResponseMsg(String responseMsg) { this.responseMsg = responseMsg; return this; } public ResponseVO setData(T data) { this.data = data; return this; } public T getData() { return data; } }
public enum ResponseState { SUCCESS("10001", "请求成功"), CHECKERROR("10002","参数验证失败"),
import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Valid { Class>[] value() default {}; Class>[] group() default {}; }