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.
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.
@JsonIgnoreProperties is used at class level to mark a property or list of properties to be ignored.
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.
@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.
Set ignoreUnknown
to true
and define the names of properties to ignore in the value
element:
@JsonIgnoreProperties(ignoreUnknown = true, value = {"hibernateLazyInitializer", "handler", "created"})
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 isOverride
:@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() { ... }
To ignore unknown properties, you also could do:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With