从@Valid 验证中排除一些字段

exclude some fields from @Valid validation(从@Valid 验证中排除一些字段)
本文介绍了从@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 验证中排除一些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)