使用Hibernate Validator验证double和float值-bean验证 [英] Validating double and float values using Hibernate Validator - bean validation

查看:877
本文介绍了使用Hibernate Validator验证double和float值-bean验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来验证Spring命令bean中的java.lang.Double字段的最大值和最小值(一个值必须在给定的值范围之间),

I'm looking for a way to validate a java.lang.Double field in the Spring command bean for its maximum and minimum values (a value must lie between a given range of values) like,

public final class WeightBean
{
     @Max(groups={ValidationGroup.class}, value=Double.MAX_VALUE, message="some key or default message")
     @Min(groups={ValidationGroup.class}, value=1D, message="some key or default message")
     private Double txtWeight;  //Getter and setter.

     public interface ValidationGroup{}         
}

但是 @Max @Min 不能使用java.lang.Double值.

请注意,由于舍入错误,不支持double和float (某些提供商可能会提供一些近似的支持)

Note that double and float are not supported due to rounding errors (some providers might provide some approximative support)

那么验证此类字段的方式是什么?

So what is the way of validating such fields?

我正在使用Spring 3.2.0和Hibernate Validator 4.3.1 CR1.

I'm working with Spring 3.2.0 and Hibernate Validator 4.3.1 CR1.

推荐答案

您可以使用批注,但可能会得到错误的结果,具体取决于.在很多情况下,这是double和imo的普遍问题,应避免使用_Double_s.也许切换到其他类型是最好的解决方案?例如 BigDecimal ?

You can use the annotation, but you might get false results depending. This is a general problem with doubles and imo in many cases _Double_s should be avoided. Maybe switching to a different type is the best solution? BigDecimal for example?

这篇关于使用Hibernate Validator验证double和float值-bean验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆