Marcin Erbel
Marcin Erbel

Reputation: 1643

Jackson JsonMappingException: Invalid type id

I've a model with field annotated as a:

@JsonTypeInfo(use = CLASS)
private Object dudClass;

The main problem is when I try to deserialize this object and I don't have this dudClass instance on classpath I will receive an exception:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Invalid type id 'pl.erbel.DudClass' (for id type 'Id.class'): no such class found.

Is it any easy way to just ignore this exception? I don't want to ignore serialization/deserialization cause I need this in different module. I just have a two clients: one with DudClass on classpath and the seconde one without that class.

Upvotes: 3

Views: 5632

Answers (1)

Coder
Coder

Reputation: 2239

It might not be the perfect solution but this is a work around. Instead of having dudClass in your POJO you can make use of the additional properties using JsonAnyGetter and JsonAnySetter. You need to remove the dudClass from your parent class and include the following code

@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);
}

What this does is while deserializing instead of trying to set the dudClass everytime it sets any additional objects that comes along in the response to the Map with object name as the key (in your case it's dudClass) and the object contents as the value. You just have see if the Map has dudClass key present or not.

Let me know if this doesn't answer your issue or need clarification!

Upvotes: 3

Related Questions