Estou tendo dificuldades para pegar uma lista de objetos em JSON e transformar em objeto usando o GSON:
Código do WS:
@GET
@Path("/buscar/")
@Produces(MediaType.APPLICATION_JSON)
public Response buscar() throws IOException, SQLException{
AlunoDAO dao = new AlunoDAO();
List<Aluno> lista = dao.trazerListaAlunos();
for(Aluno a : lista){
a.setD1(read(a.getDigital()));
a.setD2(read(a.getDigital2()));
}
GenericEntity entity = new GenericEntity<List><Aluno>>(lista) {};
return Response.ok(entity).build();
return lista;
}
Código cliente.Não dá erro algum,apenas quando tento transformar retorna o objeto nulo:
static void buscar() throws Exception{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(
"http://localhost:8080/fingerRest/rest/digital/buscar/");
get.addHeader("accept", "application/json");
get.addHeader("Content-Type", "application/json");
get.setURI(new java.net.URI("http://localhost:8080/fingerRest/rest/digital/buscar/" ));
HttpResponse response = null ;
try{
response =httpClient.execute(get);
Gson gson = new Gson();
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
List<Aluno> lista = new ArrayList<Aluno>();
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
Aluno a = gson.fromJson(output, Aluno.class);
lista.add(a);
}
for(Aluno a : lista){
System.out.println("ALUNO::"+a.getNome());
}
}catch(Exception e){
e.printStackTrace();
}
}