1

I have a rest web service written in spring mvc and using jackson json for the output. To ignore an object while serialising to json, I know I can annotate the field or class with @Jsonignore. But I have an array of objects the I want to return and whether to ignore some objects from the final json output depends on some condition. For example: ignore if the object.hasTitle() == null.

What would be the best way to achieve this in spring mvc?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chanthu
  • 1,794
  • 1
  • 15
  • 22
  • I don't think that what you are trying to do is the right way to solve the problem since the JSON library is responsible to convert data in a way that it is and hiding or filtering data should not happen on the view level. Keep in mind in the scenario that you have you are loading some data in memory and want to throw them away , so its better to not load them at all(for example with a query condition from database) , also keep in mind the JSON objects in many cases has the pagination information about how many records are in the list,... this way you also corrupt the pagination information – M2je Aug 25 '14 at 02:18

1 Answers1

0

One solution whould be to provide a custom (de)serializer where you can remove/add fields on specific conditions.

Right way to write JSON deserializer in Spring or extend it

Community
  • 1
  • 1
meleagros
  • 482
  • 1
  • 5
  • 17