Is it possible to have the @JsonProperty required dynamically set or set at call?
The reason behind this... I'm generating json files which describes a schema and defines
So, a creation requires only foo
and an update requires foo and bar
Can I make things so I can pass in something to say bar is now required?
or would I need to duplicate this code in order to have different settings for JsonProperty?
@JsonInclude(Include.NON_NULL)
public class Bean {
@JsonProperty(value="foo", required=false)
private FooProperty fooProperty;
@JsonProperty(value="bar", required=false)
private BarProperty barProperty;
//
public FooProperty getFooProperty() { return fooProperty; }
public void setFooProperty(FooProperty argFooProperty) {
this.fooProperty = argFooProperty
}
public BarProperty getBarProperty() { return barProperty; }
public void setFooProperty(BarProperty argBarProperty) {
this.barProperty = argBarProperty
}
}
You could solve this issue in couple of ways. First one would be as Franjavi
suggested you could use a mixin.
Have a mixin class which will mark your foo
as one of the ignored properties. In your main class mark both the fiends required and you can inject this mixin whenever this is an option field.
@JsonIgnoreProperties("foo")
public abstract class mixinClass {
}
You configure your mixin into your mapper as follows.
ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(Bean.class, mixinClass.class);
This is not always a working option whenever you are getting this response from a third party API where you might be serializing/deserializing using the default jackson mapper which are provided by them like Resttemplate.
In this case, instead of having the foo
property, you can just included whatever the properties that are present in all the responses and handle the rest of the properties using @JsonAnyGetter
and @JsonAnySetter
. You can capture this in a map with key being your object name which is in this case foo
. You need to have the following part in your parent node or whichever node that will encapsulates these optional properties.
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
Let me know if you need any further clarification!!