0

I have one client that post into my service currency just like this:

{
"id": 1,
"amount": "12.323,44"
}

how can configure my springboot to convert this in a java money field? this is my object

public class MyObject {
  private Long id;
  private Money amount;
}
Fabio Ebner
  • 2,613
  • 16
  • 50
  • 77

2 Answers2

0

Since you have asked for spring boot, and seems to be how to automatically convert while mapping in MVC, you are looking for @InitBinder coupled with @ControllerAdvice.

An example used is here in this S.O article What is the purpose of init binder in spring MVC.

Write a biding method to covert the string to money or any other object as you wish.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
0

Using jackson custom deserializer

@JsonComponent
publi class MoneyJsonDeserializer 
  extends JsonDeserializer<Money> {

    @Override
    public Money deserialize(JsonParser jsonParser, 
      DeserializationContext deserializationContext)
      throws IOException, JsonProcessingException {

        TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
        TextNode amountNode = (TextNode) treeNode.get(
          "amount");
        return new Money(amountNode.asText());
    }
}