为 BigDecimal 使用正确的注释
Use proper annotation for the 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
与注释一起使用。
如何使用正确的注释来过滤 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\"])"
}
@validation-api的正注
我有一个 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
与注释一起使用。
如何使用正确的注释来过滤 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\"])"
}
@validation-api的正注