Jackson Mapper serialize empty object instead of null

后端 未结 3 1095
梦毁少年i
梦毁少年i 2021-01-14 11:54

Say I have classes Foo

public class Foo {
    private Bar bar;
}

and Bar

public class Bar {
    private String fizz;
    pr         


        
3条回答
  •  臣服心动
    2021-01-14 12:09

    You could create a custom serializer for serializing Foo objects. Then in your custom FooSerializer implementation, you could check for a null bar value and serialize it as a default Bar instance. See https://spin.atomicobject.com/2016/07/01/custom-serializer-jackson/ or http://www.baeldung.com/jackson-custom-serialization for some examples of how to create custom serializers.

提交回复
热议问题