I have an annotated class that implements an interface with a read-only property named .id()
that is generic so I can pull different types of ids in other parts of the program.
This entire interface should be ignored by Jackson. But instead I get the following error message:
java.util.concurrent.ExecutionException
:com.fasterxml.jackson.databind.exc.InvalidDefinitionException
: Cannot construct instance ofmy.company.Identifiable
(no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: UNKNOWN; line: -1, column: -1] (through reference chain:java.util.HashSet[0]
)
I have tried all the solutions found here when searching for that exception as well as the entire first two pages from Google. None of them address the exact problem I am having and I have been unable to interpalate a solution for my specific problem from any of them, short of a CustomDeserializer
which I would rather not have to write.
I tried using JsonTypeInfo
but I have never had to use that annotation before and can not figure out what I am supposed to do with it. The solutions that uses are slightly different from mine, where they actually want the interface property instead of ignoring it.
In some classes there is no actual id
member variable, it is just a method
to return something that is considered the id
for another part of the program. ID<String>
in some cases and ID<Integer>
in others.
I have tried annotating with @JsonProperty(access = Access.READ_ONLY)
as well as the @JsonIgnore
and the @JsonIgnoreProperties({"id"})
none of them change the exception. I also tried all the mode
settings in @JsonCreator
, none worked.
The interface is defined as:
@JsonIgnoreProperties({"id"})
public interface Identifiable<T>
{
@JsonIgnore
public T id();
}
An example class is defined as:
@JsonIgnoreProperties(ignoreUnknown = true)
public class User implements Identifiable<ID<String>>
{
@JsonCreator
public User(@JsonProperty("LoginName") final String loginName,
@JsonProperty("Title") final String name,
@JsonProperty("Email") final String email)
/* lots of irrelevant code redacted */
@JsonIgnore
@Override
public ID<String> id()
{
return new ID.from(this.name);
}
}
This what the binding code looks like:
final HttpResponse response = req.execute();
final JsonNode root = this.om.readTree(response.getContent());
final JsonNode results = root.get("d").get("results");
final Set<V> values = this.om.readValue(this.om.treeAsTokens(results),
new TypeReference<Set<V>>() {});
Where V
is User
in this case and results
is an Array
.
Deserialization of this and a couple other classes worked fine before I added the Identifiable<T>
interface, they they all broke with this same exception.
This is not an abstract
class, every method is implemented. I think I have put the @JsonIgnore
everywhere I can that is applicable.
I still get that exception and do not know how to solve it.
I have a feeling that there is something in the @JsonTypeInfo
annotation that might fix this but the examples and javadoc are not written for someone that does not already know when or how to use the annotation.