Spring cloud项目中MethodValidationPostProcessor拦截器与自定义拦截器

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

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}

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

自定义拦截器

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UserLoggedCheck {

    LoggedCheckMode mode() default LoggedCheckMode.MUST;
}

@Aspect
public 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,
        "现金部分的退款额度有误");
该类提供了统一的校验方式,同时提供支持汉字提示的提示语








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值