Jersey/Jackson @JsonIgnore on setter
Solution 1
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
:
class A {
@JsonIgnore
public Map<String,List<String>> references;
@JsonProperty
public Map<String,List<String>> getReferences() { ... }
@JsonIgnore
public void setReferences(Map<String,List<String>>) { ... }
}
Second, in order to prevent the getters from being used as setters, disable the USE_GETTERS_AS_SETTERS
feature:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
Solution 2
As of Jackson 2.6, there is a new and improved way to define read-only
and write-only
properties, using JsonProperty#access() annotation. This is recommended over use of separate JsonIgnore
and JsonProperty
annotations.
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Map<String,List<String>> references;
Solution 3
You have to make sure there is @JsonIgnore annotation on the field level as well as on the setter, and getter annotated with @JsonProperty.
public class Echo {
@Null
@JsonIgnore
private String doNotDeserialise;
private String echo;
@JsonProperty
public String getDoNotDeserialise() {
return doNotDeserialise;
}
@JsonIgnore
public void setDoNotDeserialise(String doNotDeserialise) {
this.doNotDeserialise = doNotDeserialise;
}
public String getEcho() {
return echo;
}
public void setEcho(String echo) {
this.echo = echo;
}
}
@Controller
public class EchoController {
@ResponseBody
@RequestMapping(value = "/echo", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public Echo echo(@RequestBody @Valid Echo echo) {
if (StringUtils.isEmpty(echo.getDoNotDeserialise())) {
echo.setDoNotDeserialise("Value is set by the server, not by the client!");
}
return echo;
}
}
- If you submit a JSON request with a “doNotDeserialise” value set to something, when JSON is deserialised to an object it will be set to null (if not I put a validation constraint on the field so it will error out)
- If you set the “doNotDeserialise” value to something on the server then it will be correctly serialised to JSON and pushed to the client
Solution 4
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:
@JsonIgnoreProperties(ignoreUnknown = true, value = { "actorsAsString",
"writersAsString", "directorsAsString", "genresAsString" })
Related videos on Youtube
guerilla
Updated on February 13, 2020Comments
-
guerilla over 4 years
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
-
guerilla over 11 yearsThat's right. The incoming data is not really in our hands. But the clients sends always references attribute in correct format so handleUnknown will never be called.
-
Pete over 11 yearsAh, my bad, forgot a little pice of information. You need
@JsonIgnoreProperties
at the POJO if you want to ignore incoming data. I'll update my post. (PS: we use this to consume facebook's REST resource and all properties sent from facebook that are not contained in our POJO will just get logged and then we decide if we want to add them or not) -
guerilla over 11 yearsBut references is in my POJO so it always matches to the setter
-
Pete over 11 yearsBut why do you keep References in your POJO if you don't need it? Found another solution that might be applicable if you really want to keep your exact structure: stackoverflow.com/questions/7421474/…
-
guerilla over 11 yearsThanks for the link. I've tried mixins before and it works not for me. The challenge is getting the references property to Read-only state. So that I can send information to the client but ignore incoming changes on this property