Consumption WS Rest returns null

Asked

Viewed 51 times

1

I am consuming a WS to get data from vehicles, the API that I was passed returns a JSON:

{
"success": true,
"message": "",
"result": [  {
        "Codigo": "1013",
        "Placa": "ABC1234",
        "Ano": 2016,
        "Uf": "SP",
        "Marca": "Marca1",
        "Modelo": "OF-1418"
    }, {
        "Codigo": "1015",
        "Placa": "ABC1244",
        "Ano": 2016,
        "Uf": "SP",
        "Marca": "Marca1",
        "Modelo": "OF-1418"
    }, {
        "Codigo": "100",
        "Placa": "KYX2244",
        "Ano": 2013,
        "Uf": "SP",
        "Marca": "Mbb",
        "Modelo": "OF-1313"
    }
  ]
}

I’m using restTemplate from Spring and to turn JSON into an object I analyzed JSON and created three classes:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Result {

   private String codigo;
   private String placa;    
   private Integer ano; 
   private String uf;   
   private String marca;    
   private String modelo;   


@JsonIgnoreProperties(ignoreUnknown = true)
public class RestResponse {

   private Boolean success;
   private String message;
   private Result[] result;


public class Response {

   @JsonProperty
   private RestResponse RestResponse;

And to consume in my service class I did so:

    Response response = restTemplate.getForObject(url, Response.class);

    System.out.println("Retorno: " + response.toString());

Only that the return is null.

Where I’m going wrong?

Updating of the code

I eliminated the Response class, I was left with only two classes to Restresponse and Result now realize that something has already changed I’m calling it that

RestResponse response = restTemplate.getForObject(url, RestResponse.class);

when observing the object Answer if the sucess attribute and message now has content but the attribute Result[] result I check it

Result[] array = response.getResult();
System.out.println("Tamanho do array: " + array.length);

and the array has size plus qdo itero over it the attributes are null.

1 answer

1

I made two changes to the first one I implemented Serializable and according to JSON the key/value pair the key it starts with Uppercase so I entered @Jsonproperty for each attribute @Jsonproperty("Code") private String code; @Jsonproperty("Board") private String board; .......

Browser other questions tagged

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