user1242321
user1242321

Reputation: 1696

@JsonIgnoreProperties usage for both known and unknown properties

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.

Upvotes: 34

Views: 74372

Answers (1)

cassiomolin
cassiomolin

Reputation: 130927

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);

Upvotes: 52

Related Questions