首页 > 解决方案 > 为 BigDecimal 使用正确的注释

问题描述

我有一个POJO下面提供的 Lombok 注释,

@Setter
@Getter
public class OrderDto extends BaseDto {

    @JsonProperty( "products" )
    private final List<String> products;

    @JsonProperty( "basket_items" )
    private final List<BasketItemDto> basketItems;

    @JsonProperty( "timestamp" )
    @MockLocalDateTime( ignoreMillis = true )
    @JsonDeserialize( using = JavaOffsetDateTimeDeserializer.class )
    @JsonSerialize( using = JavaOffsetDateTimeSerializer.class )
    private OffsetDateTime timestamp;

    @JsonProperty( "amount" )
    @Min( value = 0L)
    private BigDecimal amount;

    @JsonProperty( "shop_id" )
    private Integer shopId;

}

我想该amount字段接受大于零的值。但是,当我应用注释时@Min( value = 0L)并为请求提供负值时,应用程序不会中断。

我认为原因是amount具有类型,BigDecimal然后我使用Long注释。

如何使用正确的注释来过滤 a 的值BigDecimal

更新

我尝试使用@DecimalMin("0.00")该应用程序仍然没有中断。但是,当我提供"amount": 05前导零时,我会收到消息,

{
    "success": false,
    "message": "JSON parse error: Invalid numeric value: Leading zeroes not allowed; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid numeric value: Leading zeroes not allowed\n at [Source: (PushbackInputStream); line: 4, column: 9] (through reference chain: com.xyz.bbb.dto.request.RequestDto[\"order\"])"
}

标签: javaannotationslombok

解决方案


验证API的@Positive注解


推荐阅读