Java to Jackson JSON serialization: Money fields

后端 未结 7 1440
臣服心动
臣服心动 2020-12-04 12:24

Currently, I\'m using Jackson to send out JSON results from my Spring-based web application.

The problem I\'m having is trying to get all money fields to output with

7条回答
  •  眼角桃花
    2020-12-04 12:48

    Instead of setting the @JsonSerialize on each member or getter you can configure a module that use a custome serializer for a certain type:

    SimpleModule module = new SimpleModule();
    module.addSerializer(BigInteger.class, new ToStringSerializer());
    objectMapper.registerModule(module);
    

    In the above example, I used the to string serializer to serialize BigIntegers (since javascript can not handle such numeric values).

提交回复
热议问题