Suppress wrapper object when serializing Java object into JSON using Jackson

后端 未结 5 2140
时光取名叫无心
时光取名叫无心 2021-02-15 00:02

I have a web service that returns a list as JSON. It uses Jackson to map a List of Java POJOs into JSON. The problem is that the JSON representation has a wrapper object around

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-15 00:41

    Honestly, I wouldn't be too quick to try to fix this problem as having the wrapper does create a situation where your code is more extensible. Should you expand this in the future to return other objects, your clients consuming this web service most likely won't need to change the implementation.

    However, if all clients expect an array that is unnamed, adding more properties in the future outside of that array may break the uniform interface.

    With that said, everyone has their reasons for wanting to do something a certain way. What does the object look like that you are serializing? Are you serializing an object that contains an array, or are you serializing the actual array itself? If your POJO contains an array, then maybe the solution is to pull the array out of the POJO and serialize the array instead.

提交回复
热议问题