i have an class with the following annotations:
class A {
public Map<String,List<String>> references;
@JsonProperty
public Map<String,List<String>> getReferences() {
...
}
@JsonIgnore
public void setReferences(Map<String,List<String>>) {
}
...
}
}
What I try is to ignore the json on deserialization. But it doesn't work. Always when JSON String arrives the Jackson lib fill the references attribute. If I use only the @JsonIgnore annotation the getter doesn't work. Are there any solutions for this problem?
Thanks
I can only think of a non-jackson solution, to use a base class that does not have references for the mapping and then cast to the actual class:
Why do you even send the References if you don't want them?
Or is the incoming data out of your hands and you just want to avoid the mapping exception telling you that jackson cannot find a property to set for incoming references? For that we use a base class which all of our Json model classes inherit:
Then in the POJO you add
@JsonIgnoreProperties
so that incoming properties will get forwarded tohandleUnknown()
edit
This SO Thread describes how to use Mixins. This might be the solution, if you want to keep your structure exactly as it is, but I have not tried it.
I think there are two key pieces that should enable you to have "read-only collections" as desired. First, in addition to ignoring the setter, ensure that your field is also marked with
@JsonIgnore
:Second, in order to prevent the getters from being used as setters, disable the
USE_GETTERS_AS_SETTERS
feature:You have to make sure there is @JsonIgnore annotation on the field level as well as on the setter, and getter annotated with @JsonProperty.
As of Jackson 2.6, there is a new and improved way to define
read-only
andwrite-only
properties, using JsonProperty#access() annotation. This is recommended over use of separateJsonIgnore
andJsonProperty
annotations.I used
@JsonIgnore
on my getter and it didn't work and I couldn't configure the mapper (I was using Jackson Jaxrs providers). This worked for me: