2

My POJO class has @JsonIgnore on the declaration of a field, not on the getter and setter methods. It is a generated file and I can't change much in it.

How do I ignore that field while writing using JsonGenerator.Setting? Using @JsonIgnore on getters and setters works. But can't modify the generated POJO class.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Shubham Kumar
  • 167
  • 2
  • 3
  • 10
  • after generating the class set that property to null. – Anoop LL Sep 30 '16 at 10:59
  • The values are being set from the db. I just want to ignore those fields using JsonGenerator. i.e I want to ignore them even if they have values. – Shubham Kumar Sep 30 '16 at 11:11
  • Are you trying to generate list of objects or single object? – Anoop LL Sep 30 '16 at 11:43
  • Have you tried the approach I mentioned in [my answer](http://stackoverflow.com/a/39790836/1426227)? Could you please let me know if it works? – cassiomolin Oct 04 '16 at 13:56
  • Do you mean you have 10 variables in class and the data provided may not contain 10 variables. In that case, give @JsonIgnoreProperties(ignoreUnknown = true) on top of the class. – HARDI Oct 04 '16 at 20:24
  • @HARDI: The data provided contains those variables but I dont want to display it. – Shubham Kumar Oct 25 '16 at 09:33
  • @AnoopLL: Yes that works! But I dont want to remove the values which those variables contain! Just dont want to display it without changing the generated file. – Shubham Kumar Oct 25 '16 at 09:38
  • @CássioMazzochiMolin: It worked but it was not a solution to my problem. Anyways, accepting Anoops answer. – Shubham Kumar Oct 25 '16 at 09:58

2 Answers2

4

Configuring Jackson to use only field annotations

Once the annotations are placed on the fields, you can configure ObjectMapper to use only the fields annotations, ignoring the annotations of getters and setters methods:

ObjectMapper mapper = new ObjectMapper();    
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

Jackson mix-in annotations

It's a great alternative when modifying your POJOs is not an option. You can think of it as a kind of aspect-oriented way of adding more annotations during runtime, to augment statically defined ones.

Define a mix-in annotation interface (class would do as well):

public interface FooMixIn {

    @JsonIgnore
    String getBar();
}

Then configure ObjectMapper to use the defined interface (or class) as a mix-in for your POJO:

ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT)
                                        .addMixIn(Foo.class, FooMixIn.class); 

Here are some usage considerations:

  • All annotation sets that Jackson recognizes can be mixed in.
  • All kinds of annotations (member method, static method, field, constructor annotations) can be mixed in.
  • Only method (and field) name and signature are used for matching annotations: access definitions (private, protected, ...) and method implementations are ignored.

For more details, check this page.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

Defining those fields as transients with avoid them for being serialized no matter what annotation for json you uses...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97