Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonIgnoreProperties usage for both known and unknown properties

Tags:

java

json

jackson

My current annotation for ignoring the known properties for a JPA entity is:

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler","created","updated","createdBy","lastUpdatedBy"}) 

In addition to ignoring these class properties, I would also like to ignore any unknown properties that the server receives. I know the alone way to ignore the unknown properties by the following annotation:

@JsonIgnoreProperties(ignoreUnknown=true) 

But not sure how to add this to my current annotation given above. I tried multiple methods ås below but none seem to work and I could not find an example online for this scenario.

Any example or leads on documentation would also help.

like image 779
user1242321 Avatar asked Jun 01 '17 22:06

user1242321


People also ask

Does Jackson ignore unknown properties?

There are two ways to ignore Unknown fields are provided by Jackson API. Both approaches will be discussed here and we will also see how to use them and when to use @JsonIgnoreProperties and when to ignore unknown fields in JSON globally at the ObjectMapper level.

Why do we use @JsonIgnoreProperties?

@JsonIgnoreProperties is used at class level to mark a property or list of properties to be ignored.

How do I ignore properties in ObjectMapper?

ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.

What is the use of @JsonProperty?

@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.


1 Answers

Set ignoreUnknown to true and define the names of properties to ignore in the value element:

@JsonIgnoreProperties(ignoreUnknown = true,                        value = {"hibernateLazyInitializer", "handler", "created"}) 

How does it work?

Have a look at this quote from the documentation (highlight is mine):

In its simplest form, an annotation looks like the following:

@Entity 

The at sign character (@) indicates to the compiler that what follows is an annotation. In the following example, the annotation's name is Override:

@Override void mySuperMethod() { ... } 

The annotation can include elements, which can be named or unnamed, and there are values for those elements:

@Author(name = "Benjamin Franklin", date = "3/27/2003") class MyClass() { ... } 

or

@SuppressWarnings(value = "unchecked") void myMethod() { ... } 

If there is just one element named value, then the name can be omitted, as in:

@SuppressWarnings("unchecked") void myMethod() { ... } 

Other way to handle unknown properties

To ignore unknown properties, you also could do:

ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
like image 83
cassiomolin Avatar answered Oct 05 '22 11:10

cassiomolin