0

I'm using Spring Boot and spring-web and have the following dto:

public class InstructionDto {


    private Long id;
    private String scanningStatus;
    private LocalDate dateSubmitted;
    private InstructionTypeDto type;
    private InstructionStatus status;
    @DecimalMax(value = "1000000000.0", message = "Amount must be less than 1000000000")
    private Double amount;
    private String description;
    private Currency currency;
    private LocalDate actionByDate;
    private LocalDate dateResolved;
    private LocalDate cancellationDate;
}

With amount field of Double type.

Whenever I try to get Instruction resource with amount > 10000000 I get something like:

{
  "id": 31,
  "scanningStatus": "",
  "dateSubmitted": "2020-08-17",
  "type": {
    "id": 1,
    "name": "New Type"
  },
  "status": "SENT",
  "amount": 1.0E8,
  "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean at tortor sit amet est finibus tempus. Vivamus dictum, quam in egestas mollis, nibh libero bibendum lorem, non malesuada enim risus ac risus. Fusce sed mi quis risus semper orci aliquam",
  "currency": "USD",
  "actionByDate": "2020-08-31",
  "dateResolved": null,
  "cancellationDate": null,
}

So amount = 1.0E8, however, I would prefer amount=10000000.

Is it possible to get it serialized in a proper format somehow?

Thanks

Andrey Yaskulsky
  • 2,458
  • 9
  • 39
  • 81
  • The simplest way is using a `BigDecimal` instead of `Double`. – Łukasz Olszewski Aug 17 '20 at 14:03
  • I would also prefer that, but don't want to change underlying implementation as I have a couple of layers within the application so I would need to change implementation of converters between the layers – Andrey Yaskulsky Aug 17 '20 at 14:05
  • @AndreyYaskulsky Write a custom serializer to convert into BigDecimal or desired format and add in objectmapper for Double class [Ex](https://stackoverflow.com/a/44254670/4207306) – Eklavya Aug 17 '20 at 14:19
  • 1
    Does this answer your question? [Serialize a Double to 2 decimal places using Jackson](https://stackoverflow.com/questions/11520781/serialize-a-double-to-2-decimal-places-using-jackson) – Eklavya Aug 17 '20 at 14:39

1 Answers1

0

The solution is to write custom serializer:

static class DecimalJsonSerializer extends JsonSerializer<Double> {
    @Override
    public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        jgen.writeNumber(String.format("%.2f", value));
    }
}

And then apply it on desired field:

public class InstructionDto {
    ...
    @DecimalMax(value = "1000000000.0", message = "Amount must be less than 1000000000")
    @JsonSerialize(using = DecimalJsonSerializer.class)
    private Double amount;
    ...
}
Eklavya
  • 17,618
  • 4
  • 28
  • 57
Andrey Yaskulsky
  • 2,458
  • 9
  • 39
  • 81
  • Remove unnecessary codes to focus. If you want the same behavior for all double field in application then you can add the serializer in objectmapper rather apply on each field. – Eklavya Aug 17 '20 at 14:37