Reputation: 1270
I have a simple interface with getter and setter for a property.
public interface HasMoney {
Money getMoney();
void setMoney(Money money);
}
I have another class UserAccount which implements this interface.
public class UserAccount implements HasMoney {
private Money money;
@Override
Money getMoney() // fill in the blanks
@Override
void setMoney(Money money) // fill in the blanks
}
My problem is that I want to serialize the money property but ignore while deserializing it i.e., dont accept any values from the user for this property. I have tried @JsonIgnore on setter and @JsonIgnore(false) on the getter, it does ignore it but it does so while serializing it also.
I tried @JsonIgnore on the setter and @JsonProperty on the getter just to explicitly tell Jackson that we intend to track this property, that seems to crash the application when money property is sent to the server and Jackson tries to deserialize it throwing up MalformedJsonException : cannot construct object of type Money.
The most wierd thing is that putting @JsonIgnore on the setter and @JsonProperty on the setter works for most cases when the property is primitive.
Upvotes: 62
Views: 100544
Reputation: 1616
With Jackson 2.10 you can implement a read-only field like this:
public class UserAccount implements HasMoney {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Money money;
// getter and setter
}
@JsonIgnoreProperties(ignoreUnknown = true) // to ignore ALL unknown properties
// OR
@JsonIgnoreProperties(value = {"money"}, allowGetters = true) // to ignore only 'money' input
public class UserAccount implements HasMoney {
@JsonProperty
public Money getMoney() {
// some calculation
}
}
The value will be serialized but ignored during deserialization.
Upvotes: 14
Reputation: 81
Apparently my solution is late, but definitely will be helpful for others.
Prehistory: in my project there is a class reading a JSON string directly into an entity. The JSON contains a property which is not a variable of the class. Because of
objectMapper.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
an instance of the entity will not be created during deserialization (an exception in such a case is desired in our project).
Solution:
objectMapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException {
if( (propertyName.equals("propertyToBeIgnored") && beanOrClass.getClass().equals(ClassOfTheProperty.class)) {
p.skipChildren();
return true;
} else {
return false;
}
}
});
Upvotes: 5
Reputation: 3755
In a case of you don't own or can't change the class by adding @JsonIgnore
annotation, you would get expected result by using mixin starting from version 2.5 in your implementation.
public abstract class HasMoneyMixin {
@JsonIgnore
public abstract Money getMoney();
}
configure mapper to use mixin,
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(HasMoney.class, HasMoneyMixin.class);
// avoid failing if all properties are empty
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Upvotes: 6
Reputation: 80603
Ok, so the behavior of @JsonIgnore was radically changed from 1.9 onwards (for the worse imo). Without going into the devilish details of why your property is not being ignore during deserialization, try this code to fix it:
public class UserAccount implements HasMoney {
@JsonIgnore
private BigDecimal money;
// Other variable declarations, constructors
@Override
@JsonProperty
public BigDecimal getMoney() {
return money;
}
@JsonIgnore
@Override
public void setMoney(final BigDecimal money) {
this.money = money;
}
// Other getters/setters
}
Note the use of @JsonIgnore
on the field - its required for a working solution.
Note: depending on your environment and use case, you may need additional configuration on your ObjectMapper instance, for example, USE_GETTERS_AS_SETTERS, AUTO_DETECT_GETTERS, AUTO_DETECT_SETTERS etc.
Upvotes: 35
Reputation: 1256
Version 2.6.0+ allows this to be done with @JsonIgnoreProperties at the class level.
@JsonIgnoreProperties(value={ "money" }, allowGetters=true)
Take a look at this closed issue: https://github.com/FasterXML/jackson-databind/issues/95
Upvotes: 80