【问题标题】:How to validate number of digits for json to BigDecimal, but just the fraction digits?如何验证 json 到 BigDecimal 的位数,但只是小数位数?
【发布时间】:2019-08-20 16:06:50
【问题描述】:

我想将一个 json 字符串反序列化为 BigDecimal,并且只验证小数位数:

@JsonProperty(value = "amount")
@Digits(integer = 9999, fraction = 2, message = "amount must contain 2 decimal places")
private BigDecimal amount;

我可以做这样的事情,但不对整数数字进行任何验证吗?

【问题讨论】:

  • Nope - "注解@Digits 必须定义属性整数"
  • 我明白这一点,但是是否有其他方法可以进行验证,即使用 @Digits 注释以外的其他方法?

标签: java json validation jackson bigdecimal


【解决方案1】:

我将其用作解决方法:

@JsonProperty(value = "amount")
@Digits(integer = Integer.MAX_VALUE, fraction = 2, message = "amount must contain two decimal places")
private BigDecimal amount;

【讨论】:

  • 如果这是一种变通方法,它与您的理想情况有何不同?
猜你喜欢
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 2011-05-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
相关资源
最近更新 更多