Error deserializing a Json List in Java

0

This is as follows, I get a list in JSON format. But it seems that there is some kind of nonconformity in the way the JSON is mounted. Follow the code ... I have a client class that looks like this.

public final class Cliente {

    private Integer Id;
    private String nome;
    private String cpf;
    private String endereco;
    //Com os Getters and Setters
}

The class with the Main method is like this.

package br.com.pecapreco.pecas;

import java.util.List;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import br.com.pecapreco.model.Cliente;

public class Programa {

    public List<Cliente> listar() {

        Client client = Client.create();
        WebResource webResource = client.resource("http://localhost:8080/prjRestful3/cliente/");
        return webResource.path("listarTodos").get(new GenericType<List<Cliente>>() {
        });
    }

    public static void main(String[] args) {
        Programa pg = new Programa();
        List<Cliente> list = pg.listar();
        for (Cliente cliente : list) {

            System.out.println(cliente.getNome());
        }
    }
}

And the following error is displayed in the console:

  

Exception in thread "main" javax.ws.rs.WebApplicationException: com.owlike.genson.JsonBindingException: Could not deserialize to type interface java.util.List       at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom (GensonJsonConverter.java:127)       at com.sun.jersey.api.client.ClientResponse.getEntity (ClientResponse.java:634)       at com.sun.jersey.api.client.ClientResponse.getEntity (ClientResponse.java:604)       at com.sun.jersey.api.client.WebResource.handle (WebResource.java:698)       at com.sun.jersey.api.client.WebResource.get (WebResource.java:198)       at.wikipedia.org/wiki/index.php       at.wikipedia.org/wiki/index.php   Caused by: java.util.List       at com.owlike.genson.Genson.deserialize (Genson.java:384)       at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom (GensonJsonConverter.java:125)       ... more   Caused by: com.owlike.genson.stream.JsonStreamException: Illegal character at row 0 and column 0 expected [but read '{'!       at com.owlike.genson.stream.JsonReader.newWrongTokenException (JsonReader.java:942)       at com.owlike.genson.stream.JsonReader.begin (JsonReader.java:418)       at com.owlike.genson.stream.JsonReader.beginArray (JsonReader.java:149)       at com.owlike.genson.convert.DefaultConverters $ CollectionConverter.deserialize (DefaultConverters.java:172)       at com.owlike.genson.convert.DefaultConverters $ CollectionConverter.deserialize (DefaultConverters.java:159)       at com.owlike.genson.convert.NullConverterFactory $ NullConverterWrapper.deserialize (NullConverterFactory.java:77)       at com.owlike.genson.Genson.deserialize (Genson.java:382)       ... 7 more

EDIT 1:

This is the Json I'm getting:

{"cpf": "314-63-6517", "address": "65 Dennis Trail", "id": "42", "name": "Aigneis McTeer"}, {"cpf": "836-24-5953", "address": "21 Doe Crossing Court", "id": "5", "name": "Aile Drabble"}}}}     

asked by anonymous 04.11.2018 / 01:57

1 answer

0

The Json you mounted does not reflect the Client class. At the beginning of Json it indicates that it has a list inside the property "client", a fact that does not happen and "id" in Json is different from "Id" in the class. Try passing Json in the following format:

    [{"cpf":"314-63-6517","endereco":"65 Dennis Trail","Id":42,"nome":"Aigneis McTeer"},
{"cpf":"836-24-5953","endereco":"21 Doe Crossing Court","Id":5,"nome":"Aile Drabble"}]
    
05.11.2018 / 01:47