Is Jackson really unable to deserialize json into a generic type?
I tried using the same approach but I haven't annotated my model class. It worked fine for me.
This is my model class
public class BasicMessage<T extends Serializable> implements Message<T> {
private MessageHeader messageHeader = new MessageHeader();
private T payload;
public MessageHeader getHeaders() {
return messageHeader;
}
public Object getHeader(String key) {
return messageHeader.get(key);
}
public Object addHeader(String key, Object header) {
return messageHeader.put(key, header);
}
public T getPayload() {
return payload;
}
public void setPayload(T messageBody) {
this.payload = messageBody;
}
}
And I used the following method for deserializing the payload
public static <T extends Serializable> BasicMessage<T> getConcreteMessageType(String jsonString, Class<T> classType) {
try {
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(BasicMessage.class, classType);
return mapper.readValue(jsonString, javaType);
} catch (IOException e) {
}
}
where jsonString contains the BasicMessageObject in a string.
If you programmatically pick up the java.lang.reflect.Type
from for instance a method return type or a field, then it is easiest to use
Type type = obj.getClass().getDeclaredField( "aKnownFieldName" ).getGenericType();
// or
Type type = obj.getClass().getDeclaredMethod( "aKnownMethod" ).getGenericReturnType();
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructType( type );
Object value = mapper.readValue( json, javaType );
A fully nested JavaType is created, so Controller<PID<Temperature,Double>>>
will be deserialzed correctly.
You need to add some annotations on the constructor to tell Jackson how to build the object. The following worked for me:
public class AgentResponse<T> {
private T result;
@JsonCreator
public AgentResponse(@JsonProperty("result") T result) {
this.result = result;
}
public T getResult() {
return result;
}
}
Without the @JsonCreator
annotation, Jackson cannot know to call this constructor. And without the @JsonProperty
annotation, Jackson does not know that the first argument of the constructor maps to the result
property.