Jackson: How to add custom property to the JSON without modifying the POJO

后端 未结 11 1310
南方客
南方客 2020-11-27 03:10

I am developing a REST interface for my app using Jackson to serialize my POJO domain objects to JSON representation. I want to customize the serialization for some types to

11条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 03:46

    Another and perhaps the most simple solution:

    Make serialisation a 2-step process. First create a Map like:

    Map map = req.mapper().convertValue( result, new TypeReference>() {} );
    

    then add the properties you want like:

    map.put( "custom", "value" );
    

    then serialise this to json:

    String json = req.mapper().writeValueAsString( map );
    

提交回复
热议问题