Usando Jersey estoy definiendo un servicio como:Jackson ObjectMapper con los genéricos a POJO en lugar de LinkedHashMap
@Path("/studentIds")
public void writeList(JsonArray<Long> studentIds){
//iterate over studentIds and save them
}
Dónde JSONArray es:
public class JsonArray<T> extends ArrayList<T> {
public JsonArray(String v) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
TypeReference<ArrayList<T>> typeRef = new TypeReference<ArrayList<T>>() {};
ArrayList<T> list = objectMapper.readValue(v, typeRef);
for (T x : list) {
this.add((T) x);
}
}
}
Esto funciona bien, pero cuando lo hago algo más complicado:
@Path("/studentIds")
public void writeList(JsonArray<TypeIdentifier> studentIds){
//iterate over studentIds and save them by type
}
Cuando el Bean es un POJO simple, como
public class TypeIdentifier {
private String type;
private Long id;
//getters/setters
}
Todo se rompe horriblemente. Convierte todo a LinkedHashMap en lugar de al objeto real. Puedo hacer que funcione si puedo crear manualmente una clase como:
public class JsonArrayTypeIdentifier extends ArrayList<TypeIdentifier> {
public JsonArrayTypeIdentifier(String v) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new MappingJsonFactory());
TypeReference<ArrayList<TypeIdentifier>> typeRef = new TypeReference<ArrayList<TypeIdentifier>>(){};
ArrayList<TypeIdentifier> list = objectMapper.readValue(v, typeRef);
for(TypeIdentifier x : list){
this.add((TypeIdentifier) x);
}
}
}
Pero yo estoy tratando de mantener este bonito y genérica sin añadir clases adicionales por todas partes. ¿Alguna pista sobre por qué esto está sucediendo solo con la versión genérica?
Ver posible duplicar con una respuesta aceptada: http://stackoverflow.com/questions/6062011/jackson-is-not-deserialising-a-generic-list-that-it-has-serialised –