Jackson - ignore Map superclass when serializing

前端 未结 3 1681
你的背包
你的背包 2020-12-18 08:27

I have a few model classes that extend LinkedHashMap: they define getters and setters which wrap the Map\'s get and put methods. I am tryi

3条回答
  •  抹茶落季
    2020-12-18 08:55

    I have a few model classes that extend LinkedHashMap: they define getters and setters which wrap the Map's get and put methods

    This is a classic example of when not to use inheritance: you're finding that some other piece of code (i.e. Jackson) is treating your class like an instance of its superclass, which isn't what you want it to do. In cases like these (and also in general), it's usually better to use composition rather than inheritance.

    I recommend rewriting your model class to contain a map, rather than extending one. You get much more control than way, and the resulting model is less brittle. If you need to view your model as a Map, then implement an asMap method (or something similar) which renders that view.

提交回复
热议问题