最簡(jiǎn)潔、最少侵入實(shí)現(xiàn)接口入?yún)⑿r?yàn)
原創(chuàng):http://www.reibang.com/p/a9e96e815b53
目錄
- 實(shí)現(xiàn)入?yún)⑿r?yàn)Aop
- 使用入?yún)⑿r?yàn)Aop
- 演示
- 附
實(shí)現(xiàn)入?yún)⑿r?yàn)Aop
1.定義一個(gè)入?yún)⒒?lèi)
package com.param.validate.aop;
/**
* 基礎(chǔ)請(qǐng)求基類(lèi)
*
* @author: zetting
* @date: 2018/8/22 7:15
*/
public class Request {
}
2.實(shí)現(xiàn)入?yún)⑿r?yàn)切面
package com.param.validate.aop;
import com.param.validate.base.BusinessCode;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.groups.Default;
import java.util.Iterator;
import java.util.Set;
/**
* controller 層入?yún)⑿r?yàn)切面
*
* @Author zetting
* @Date 2017/11/15 21:50
**/
@Order(2)
@Component
@Aspect
public class ValidatorAspect {
@Resource
private LocalValidatorFactoryBean localValidatorFactoryBean;
public ValidatorAspect() {
}
/**
* 方式1:切入點(diǎn)(
*/
@Pointcut(
"@annotation(org.springframework.web.bind.annotation.RequestMapping)" +
"||@annotation(org.springframework.web.bind.annotation.GetMapping)" +
"||@annotation(org.springframework.web.bind.annotation.PostMapping)" +
"||@annotation(org.springframework.web.bind.annotation.PutMapping)"
)
// @Pointcut("execution(* com.*..controller.*.*(..))")//方式2
private void parameterPointCut() {
}
/**
* 處理
*
* @param joinPoint
* @param request
*/
@Before("parameterPointCut() && args(request,..)")
public void validateParameter(JoinPoint joinPoint, Request request) {
Set<ConstraintViolation<Request>> validErrors = this.localValidatorFactoryBean.validate(request, new Class[]{Default.class});
Iterator iterator = validErrors.iterator();
StringBuilder errorMsg = new StringBuilder();
while (iterator.hasNext()) {
ConstraintViolation constraintViolation = (ConstraintViolation) iterator.next();
String error = constraintViolation.getPropertyPath() + ":" + constraintViolation.getMessage();
errorMsg.append(iterator.hasNext() ? error + "; " : error);
}
if (!validErrors.isEmpty()) {
throw new BusinessException(BusinessCode.PARAM_ILLEGAL, errorMsg.toString());
}
}
}
使用入?yún)⑿r?yàn)Aop
1.入?yún)⒄?qǐng)求類(lèi)集成基類(lèi)Request并在相應(yīng)字段加上校驗(yàn)注解
package com.param.validate.modules.dto;
import com.param.validate.aop.Request;
import org.hibernate.validator.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* 入?yún)⑿r?yàn)請(qǐng)求
*
* @author: zetting
* @date: 2018/12/19 22:16
*/
public class ValidateRequest extends Request {
/**
* 用戶(hù)名
*/
@NotBlank
private String userName;
/**
*性別
*/
@NotNull
private Integer sex;
/**
* 地址
*/
private String address;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.controller層正常使用腿椎、無(wú)需添加任何東西
package com.param.validate.modules.controller;
import com.param.validate.modules.dto.ValidateRequest;
import com.param.validate.base.Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 切面實(shí)現(xiàn)入?yún)⑿r?yàn)
*/
@RestController
public class MyController {
@GetMapping(value = "/validate")
public Response validate(ValidateRequest request) {
return Response.success();
}
}
演示
1.入?yún)⒉缓戏?/p>
2.入?yún)⒑戏?/p>
附
gitee源碼:
https://gitee.com/zetting/my-gather/tree/master/springboot-param-validate