java 切面 金额 校验,JAVA语言之详解Spring AOP 实现“切面式”valid校验[Java代码]

本文主要向大家介绍了JAVA语言的详解Spring AOP 实现“切面式”valid校验,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

why:

为什么要用aop实现校验?

answer:

spring mvc 默认自带的校验机制 @Valid + BindingResult, 但这种默认实现都得在Controller方法的中去接收BindingResult,从而进行校验.

eg:

if (result.hasErrors()) {

ListallErrors = result.getAllErrors();

Listerrorlists = new ArrayList<>();

for (ObjectError objectError : allErrors) {

errorlists.add(objectError.getDefaultMessage());

}

}

复制代码

获取errorlists。这样实现的话,每个需要校验的方法都得重复调用,即使封装也是。

可能上面那么说还不能表明spring 的@Valid + BindingResult实现,我先举个“栗子”。

1. 栗子(旧版本)

1.1 接口层(IDAL)

eg: 简单的POST请求,@RequestBody接收请求数据,@Valid + BindingResult进行校验

httpMethid: POST

parameters:@RequestBody接收请求数据

valid:@Valid +BindingResult

@ResponseBody

@PostMapping("body")

public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){

//校验到错误

if (result.hasErrors()) {

ListallErrors = result.getAllErrors();

Listlists = new ArrayList<>();

for (ObjectError objectError : allErrors) {

lists.add(objectError.getDefaultMessage());

}

return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "parameter empty", lists);

}

return new ResponseVO(HttpStatus.OK.value(), "bodyPost", null);

}

复制代码

1.2 实体(vo)校验内容

@Valid + BindingResult的校验注解一大堆,网上一摸就有的!

public class TestVO {

@Getter

@Setter

@Min(value = 0,message = "请求参数isString不能小于0")

private Integer isInt;

@Getter

@Setter

@NotBlank(message = "请求参数isString不能为空")

private String isString;

}

复制代码

1.3 结果测试

2. aop校验(升级版)

可以看到若是多个像bodyPost一样都需要对body进行校验的话,那么有一坨代码就必须不断复现,即使改为父类可复用方法,也得去调用。所以左思右想还是觉得不优雅。所以有了aop进行切面校验。

2.1 接口层(IDAL)

是的!你没看错,上面那一坨代码没了,也不需要调用父类的的共用方法。就单单一个注解就完事了:@ParamValid

@ParamValid

@ResponseBody

@PostMapping("body")

public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){

return new ResponseVO("bodyPost", null);

}

复制代码

2.2 自定义注解(annotation)

这个注解也是简简单单的用于方法的注解。

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface ParamValid {}

复制代码

2.3 重点!切面实现(Aspect)

切面详解:

@Before: 使用注解方式@annotation(XX),凡是使用到所需切的注解(@ParamValid),都会调用该方法

JoinPoint: 通过JoinPoint获取方法的参数,以此获取BindingResult所校验到的内容

迁移校验封装: 将原先那一坨校验迁移到Aspect中:validRequestParams

响应校验结果:

通过RequestContextHolder获取response

获取响应OutputStream

将BindingResult封装响应

@Aspect

@Component

public class ParamValidAspect {

private static final Logger log = LoggerFactory.getLogger(ParamValidAspect.class);

@Before("@annotation(paramValid)")

public void paramValid(JoinPoint point, ParamValid paramValid) {

Object[] paramObj = point.getArgs();

if (paramObj.length > 0) {

if (paramObj[1] instanceof BindingResult) {

BindingResult result = (BindingResult) paramObj[1];

ResponseVO errorMap = this.validRequestParams(result);

if (errorMap != null) {

ServletRequestAttributes res = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletResponse response = res.getResponse();

response.setCharacterEncoding("UTF-8");

response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);

response.setStatus(HttpStatus.BAD_REQUEST.value());

OutputStream output = null;

try {

output = response.getOutputStream();

errorMap.setCode(null);

String error = new Gson().toJson(errorMap);

log.info("aop 检测到参数不规范" + error);

output.write(error.getBytes("UTF-8"));

} catch (IOException e) {

log.error(e.getMessage());

} finally {

try {

if (output != null) {

output.close();

}

} catch (IOException e) {

log.error(e.getMessage());

}

}

}

}

}

}

/**

* 校验

*/

private ResponseVO validRequestParams(BindingResult result) {

if (result.hasErrors()) {

ListallErrors = result.getAllErrors();

Listlists = new ArrayList<>();

for (ObjectError objectError : allErrors) {

lists.add(objectError.getDefaultMessage());

}

return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "parameter empty", lists);

}

return null;

}

}

复制代码

2.4 测试结果

看了上面两种结果,就可以对比出使用Spring AOP 配合@Valid + BindingResult进行校验的优点:

去除代码冗余

AOP异步处理

优化代码实现

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值