Error deserializing a Java List Json

Asked

Viewed 116 times

0

The following is, I get a list in JSON format. However, it seems that there is some kind of non-conformity in the way JSON is mounted. It follows the code... I have a client class that is as follows.

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 on the console:

Exception in thread "main" javax.ws.rs.Webapplicationexception: com.owlike.Jsonbindingexception: Could not deserialize to type interface java.util.List at com.owlike.Genson.ext.jaxrs.GensonJsonConverter.readFrom(Gensonjsonconverter.java:127) com.sun.jersey.api.client.ClientResponse.getEntity(Clientresponse.java:634) 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 br.com.pecapreco.pecas.Programa.listar(Programa.java:16) at br.com.pecapreco.pecas.Programa.main(Programa.java:22) Caused by: com.owlike.Genson.Jsonbindingexception: Could not deserialize to type interface java.util.List at com.owlike.Genson.Genson.deserialize(Genson.java:384) at com.owlike.Genson.ext.jaxrs.GensonJsonConverter.readFrom(Gensonjsonconverter.java:125) ... 6 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) with.owlike.Defaultconverters$Collectionconverter.deserialize(Defaultconverters.java:172) with.owlike.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:

{"client":[{"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"}]}

  • 1

    It was not possible to post the JSON that comes through the URL.

1 answer

0

The Json you have assembled does not reflect the Client class. At the beginning of Json indicates that it has a list within the "client" property, which does not happen and "id" in Json is different from "Id" in the class. Try to pass 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"}]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.