Java Generics JSON Unmarshalling Using Jackson via Jersey Resource

Jackson seems to unmarshal properly for objects with a specific class type.  For types with generics, for example List<Element>, I could only specify the List.class, and the Element JSON was unmarshalled as LinkedHashMaps instead of the Element.class.

I found a way to work around it and applied it to the Jersey use case for my unit test.

ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, Element.class);

String json = client().resource("/some/collection").get(String.class);
List<Element> elements = mapper.readValue(json, type);
Advertisements

One thought on “Java Generics JSON Unmarshalling Using Jackson via Jersey Resource”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s