33

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.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
user1242321
  • 1,578
  • 2
  • 18
  • 30

1 Answers1

50

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);
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Why isn't `ignoreUnknown=false` working? https://stackoverflow.com/questions/56052262/how-do-i-enable-strict-validation-of-json-jackson-requestbody-in-spring-boot – Chloe May 09 '19 at 05:09
  • @Chloe `false` is the default value https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonIgnoreProperties.html#ignoreUnknown() – charlie Oct 28 '19 at 15:38