发表于: 2020-05-26 21:58:13

1 1506


今天完成的事情:

1. 学习 Validator 的配置

https://cloud.tencent.com/developer/article/1546262


明天的计划:

1. 开始任务二

问了师兄说这个东西之后的任务还会用到,然后在网上搜索有提示说这是 spring3.1 的bug:https://stackoverflow.com/questions/9146252/spring-mvc-3-exception-an-errors-bindingresult-argument-is-expected


我今天也有尝试升级 spring 的版本,但是会各种报错,我在之后的任务会开始使用更高的 spring 版本,毕竟版本过低的问题坑了我好几次了。


遇到的问题:

1. xml 配置的时候写错

一开始这里写成 value,一直报错无法把 string 转换为 validationMessageSource


2. 验证框架正常工作,但是 spring 3.1 无法获取框架的异常信息,无法读取不到配置文件

以下是验证框架提示测一条报错信息,是我传入的 student_name 过长而引发错误。

DEBUG [qtp483422889-27] - Resolving exception from handler [public java.util.Map<java.lang.String, java.lang.Object> cn.mogeek.controller.RestfulController.addDisciple(cn.mogeek.model.Disciple,org.springframework.validation.BindingResult)]: org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 0 in method: public java.util.Map<java.lang.String, java.lang.Object> cn.mogeek.controller.RestfulController.addDisciple(cn.mogeek.model.Disciple,org.springframework.validation.BindingResult), with 1 error(s): [Field error in object 'disciple' on field 'student_name': rejected value [奇异11111111111111111111111111111111111111111111111111博士]; codes [Length.disciple.student_name,Length.student_name,Length.java.lang.String,Length]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [disciple.student_name,student_name]; arguments []; default message [student_name],20,1]; default message [disciple.student_name.length.error]]

标红的部分是我在实体类中写好的 message,框架捕捉到了错误也提示了预设的信息


log 信息提示读取不到配置文件,上面的 log 也没有把 message 替换位我配置文件中的中文信息:

DEBUG [qtp659748578-26] - ValidationMessages not found.

DEBUG [qtp659748578-26] - org.hibernate.validator.ValidationMessages found


下面贴上我的配置以及依赖:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<property name="validationMessageSource" ref="validationMessageSource"/>
</bean>
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:i18n.validationMessageSource"/>
<property name="fileEncodings" value="utf-8"/>
</bean>

<!--这里不能使用过高的版本,试了几次这是不会报错的较高版本-->

<!--数据校验框架-->

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>

controller中的方法签名,只要框架检测到错误就直接退出方法,不会执行任何方法语句:

@RequestMapping(value = "", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> addDisciple(@RequestBody @Valid Disciple disciple, BindingResult bindingResult)

下面是调用以上方法的整个 log 截图:

postman 截图:




啊,我真的太菜了


返回列表 返回列表
评论

    分享到