Следующие работы и по совету StaxMan больше не используют устаревший статический метод collectionType()
.
public class SoApp
{
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
System.out.println("Hello World!");
String s = "[{\"user\":\"TestCity\",\"role\":\"TestCountry\"},{\"user\":\"TestCity\",\"role\":\"TestCountry\"}]";
StringReader sr = new StringReader("{\"user\":\"TestCity\",\"role\":\"TestCountry\"}");
//UserRole user = mapper.readValue(sr, UserRole.class);
mapJsonToObjectList(new UserRole(),s,UserRole.class);
}
protected static <T> List<T> mapJsonToObjectList(T typeDef,String json,Class clazz) throws Exception
{
List<T> list;
ObjectMapper mapper = new ObjectMapper();
System.out.println(json);
TypeFactory t = TypeFactory.defaultInstance();
list = mapper.readValue(json, t.constructCollectionType(ArrayList.class,clazz));
System.out.println(list);
System.out.println(list.get(0).getClass());
return list;
}
}
...
public class UserRole{
private String user;
private String role;
public void setUser(String user) {
this.user = user;
}
public void setRole(String role) {
this.role = role;
}
public String getUser() {
return user;
}
public String getRole() {
return role;
}
@Override
public String toString()
{
return "UserRole [user=" + user + ", role=" + role + "]";
}
}
output ...
Hello World!
[{"user":"TestCity","role":"TestCountry"},{"user":"TestCity","role":"TestCountry"}]
[UserRole [user=TestCity, role=TestCountry], UserRole [user=TestCity, role=TestCountry]]
class com.test.so.fix.UserRole