简体   繁体   中英

Node validator could be found for constraints 'javax.validation.constraints.Size' for FLOAT

Posting this because I haven't seen a post relating to FLOAT types on this issue.

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

How do I change constraints to apply for FLOAT types?

@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;

}

From the javadoc - @Size is supported for CharSequence, Collections, Maps, Arrays. The annotations for numeric types( @Min , @Max , etc.) do not support double and float, because of rounding errors, citating javadoc - Note that double and float are not supported due to rounding errors (some providers might provide some approximative support). Depending on your provider, they might work, or might not.

Your options are:

  1. using BigDecimal - annotations for validation of numerics support it.
  2. write your own custom annotation and validator for double and float - you can use this guide , if you don't know how to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM