博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring cloud项目中MethodValidationPostProcessor拦截器与自定义拦截器
阅读量:4042 次
发布时间:2019-05-24

本文共 1445 字,大约阅读时间需要 4 分钟。

1.spring cloud项目中 Java Bean Validation 的校验,不需要再声明MethodValidationPostProcessor的@Bean,如下图

@Beanpublic MethodValidationPostProcessor methodValidationPostProcessor() {    return new MethodValidationPostProcessor();}

2.如果spring cloud的项目中Controller中声明了上述的MethodValidationPostProcessor类,那么程序启动后不再进入自己定义的拦截器。在系统启动时,如果有声明MethodValidationPostProcessor的@Bean,则不会去加载自己定义的拦截器;如果未声明MethodValidationPostProcessor的@Bean,系统会寻找自定义的拦截器。spring是存在很多默认配置,有触发因子去修改配置的。

自定义拦截器

@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface UserLoggedCheck {    LoggedCheckMode mode() default LoggedCheckMode.MUST;}

@Aspectpublic class UserLoggedInterceptor implements Ordered {    private WebFilter webFilter;    private int order;    public UserLoggedInterceptor(int order) {        super();        this.order = order;    }    public UserLoggedInterceptor(int order, WebFilter webFilter) {        super();        this.order = order;        this.webFilter = webFilter;    }    @Around("@annotation(userLoggedCheck)")    public Object pointTransactional(ProceedingJoinPoint point, UserLoggedCheck userLoggedCheck) throws Throwable {

3.spring cloud的Validation 的校验主要存在于Controller层,跟jpa的save层,进行Bean的校验。

4.方法层的校验建议使用com.google.guava包中的Preconditions类

Preconditions.checkNotNull(rebackRequest,"退款信息无效");Preconditions.checkArgument(        rebackRequest.getCash().compareTo(BigDecimal.ZERO)>=0,        "现金部分的退款额度有误");
该类提供了统一的校验方式,同时提供支持汉字提示的提示语

转载地址:http://qeadi.baihongyu.com/

你可能感兴趣的文章
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
让我做你的下一行Code
查看>>
浅析:setsockopt()改善程序的健壮性
查看>>