0

Jackson has the @JsonIgnoreProperties annotation at field level which is awesome, but sometime it can be big.

@JsonIgnoreProperties({ "name", "phone", ... "20th item"})
private User user;

I'm looking for something like:

@JsonIgnoreOtherProperties("email")
private User user;

So this would ignore all fields but email.

Is there something like @JsonIgnoreOtherProperties ?

CelinHC
  • 1,857
  • 2
  • 27
  • 36

1 Answers1

0

I don't think that exists but there are several ways of achieving what you want (other than your existing solution):

1) Using @JsonView (http://wiki.fasterxml.com/JacksonJsonViews). See What is the JSON View class in Jackson and how does it work? for an example.

2) Creating a different User view which contains a minimal set of fields, perhaps UserMinimal. It might inherit from a common interface as your other User view to show they really represent the same entity.

3) Customizing the object mapper and using a filter. See How do I exclude fields with Jackson not using annotations? for an example.

Personally, I prefer the first solution if you have a simple use case and the second if you have a more complex one. I dislike the third option as I think customizing object mappers leads down an error prone path in my experience.

Your annotation idea sounds interesting though. I would consider putting it as a feature request (I think it would go https://github.com/FasterXML/jackson-annotations/issues).

Community
  • 1
  • 1
Adam
  • 2,214
  • 1
  • 15
  • 26