exclude some fields from @Valid validation(从@Valid 验证中排除一些字段)
问题描述
我使用 Spring 的 @Valid
注释来验证带有 javax.constraints
注释的 bean 字段.
I use Spring's @Valid
annotation for validate bean's fields which annotated with javax.constraints
annotations.
但是当我需要从验证中排除某些字段时(仅适用于某些情况),我遇到了一个问题.
But I faced a problem when I need to exclude some fields from validation (only for some cases).
我进行了调查,没有发现任何有用的方法,并且大多数答案的日期为 2010-2011.这是相当令人惊讶的,因为这种情况如此普遍.
I did an investigation didn't found any usefull ways and most of answers were dated as 2010-2011. It is quite surprisingly since this situatuion is so common.
Spring 4.+ 从那时起有什么变化吗?或者也许任何人都可以分享如何打败这个的个人经验?
Is there any changes from that times for Spring 4.+? Or maybe anyone can share personal experience how to beat this?
谢谢.
推荐答案
可以使用validation group,@Validated
注释.
You can use validation group, and @Validated
annotation.
http://www 中有详细说明.javacodegeeks.com/2014/08/validation-groups-in-spring-mvc.html
该方法基于 休眠验证器组,它使您能够根据标记接口对验证进行分组并将其应用于处理程序级别,如链接中给出的示例
The approach is based on hibernate validator's groups and it enables you to group the validation based on the marker interface and apply it on handler level, example as given in the link
@RequestMapping(value = "stepTwo", method = RequestMethod.POST)
public String stepTwo(@Validated(Account.ValidationStepTwo.class) Account account, Errors errors) {
if (errors.hasErrors()) {
return VIEW_STEP_TWO;
}
return "redirect:summary";
}
这篇关于从@Valid 验证中排除一些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从@Valid 验证中排除一些字段


基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01