【问题标题】:Node validator could be found for constraints 'javax.validation.constraints.Size' for FLOAT可以为 FLOAT 的约束“javax.validation.constraints.Size”找到节点验证器
【发布时间】:2022-01-31 06:21:40
【问题描述】:

发布此内容是因为我还没有看到有关此问题的 FLOAT 类型的帖子。

No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.lang.Float'. Check configuration for 'numIngredient'

如何更改约束以应用 FLOAT 类型?

@Data
@Entity
@Table(name = "pantry")
public class Pantry {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "userID", nullable = false)
    private Long userID;

    @Column(name = "ingredients_in_pantry", nullable = true)
    private String ingredientID;

    @NotNull
    @Column(name = "number_of_ingredients", nullable = true)
    private Float numIngredient;

    //description of pantry (home, office, kitchen, grandmas, etc.)
    @Column(name = "description", nullable = true)
    private String description;

}

【问题讨论】:

    标签: java spring-boot floating-point


    【解决方案1】:

    javadoc - @Size 支持 CharSequence、Collections、Maps、Arrays。数字类型的注释(@Min@Max 等)不支持 double 和 float,因为舍入错误,引用 javadoc - Note that double and float are not supported due to rounding errors (some providers might provide some approximative support). 取决于您的提供者,它们可能有效,也可能无效。

    您的选择是:

    1. 使用BigDecimal - 用于验证数字的注释支持它。
    2. 为 double 和 float 编写您自己的自定义注释和验证器 - 如果您不知道如何使用,可以使用此 guide

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 2023-01-05
      • 2019-03-07
      相关资源
      最近更新 更多