@Valid 参数校验不生效问题解决

Java 专栏收录该内容
6 篇文章 0 订阅

@Valid 参数校验不生效问题解决

问题现象

import lombok.Data;

import javax.validation.constraints.NotNull;

@Data
public class IDDTO {

    @NotNull
    private Long id;
}
@Slf4j
@Controller
@RequestMapping("/example")
public class ExampleController {

    @POSTMapping(value = "/valid")
    public BaseResponseVO valid(@Valid IDDTO idDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("what fuck");
        }
        return BaseResponseVO.success();
    }

}

看起来像不像会打印what fuck,实际上居然不会。实际上是因为Valid Property 会在json序列化构建Java Bean的时候,所以这里的Post请求还必须是application/json 即:

    @POSTMapping(value = "/valid")
    public BaseResponseVO valid(@Valid @RequestBody IDDTO idDTO, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("what fuck");
        }
        return BaseResponseVO.success();
    }

手动检查

如果有其他情况发现@Valid不生效,提供一个手动检查的方案

import org.apache.commons.collections.CollectionUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ValidateUtil {
    private static Validator validator = Validation.buildDefaultValidatorFactory()
            .getValidator();

    public static void beanValidate(Object obj) {
        Map<String, String> validatedMsg = new HashMap<>();
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(obj);
        for (ConstraintViolation<Object> c : constraintViolations) {
            validatedMsg.put(c.getPropertyPath().toString(), c.getMessage());
        }
        if (CollectionUtils.isNotEmpty(constraintViolations)) {
            throw new Exception(validatedMsg);
        }

    }

}

即:

@Slf4j
@Controller
@RequestMapping("/example")
public class ExampleController {

    @POSTMapping(value = "/valid")
    public BaseResponseVO valid(IDDTO idDTO){
        ValidateUtil.beanValidate(idDTO);
        return BaseResponseVO.success();
    }

}

不生效原因

笔者在开发过程中发现有如下一段日志,应该可以定位为什么@Valid不生效,笔者暂时比较忙,稍后再补充分析。如果有同学有兴趣,可以先自行分析。

2020-07-11 11:44:20,468 [main] DEBUG (OptionalValidatorFactoryBean:43) - Failed to set up a Bean Validation provider
javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
	at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:271) ~[validation-api-1.1.0.Final.jar:?]
	at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:255) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean.afterPropertiesSet(OptionalValidatorFactoryBean.java:40) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1692) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) [spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) [spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668) [spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634) [spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682) [spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553) [spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494) [spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171) [spring-webmvc-4.3.17.RELEASE.jar:4.3.17.RELEASE]
	at javax.servlet.GenericServlet.init(GenericServlet.java:244) [javax.servlet-3.0.0.v201112011016.jar:?]

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值