REST API using Spring Boot

Asked

Viewed 704 times

0

How do I bring all the data from an external service using Spring Boot. I have never done this to bring the information. I have created applications using data from a database. I saw some tutorials but I did not get the expected result.

The Service returns this data:

{
    "dados": {
        "totalCount": 100,
        "dadosReais": [{
            "id": 0001,
            "anoApresentacao": 1995,
            "txtDescricao": "exemplo de descrição",
            etc...
        }]
     }
}

My mapping:

public class ClientRest{

private String dados;
private String dadosReais;
private long id;
private Date dataApresentacao;
private String txtDescricao;
//getters e setters

My GET call to the service:

public class ClienteServiceWs {

public static void main(String[] args) {
    RestTemplate restTemplate = new RestTemplateBuilder()
            .rootUri("https://www.exemplo.com.br/api/servico/v1").build();

  //ClientRest clientRest= restTemplate.getForObject("/", ClientRest.class);
  ResponseEntity<List<ClientRest>> exchange = restTemplate.exchange("/",
       HttpMethod.GET, null, new ParameterizedTypeReference<List<ClientRest>>() {
});

  System.out.println(exchange.getBody());

}

}

Return:

10:08:19.436 [main] DEBUG org.springframework.web.client.RestTemplate - HTTP GET https:/www.exemplo.com.br/api/servico/v1
10:08:19.462 [main] DEBUG org.springframework.web.client.RestTemplate - Accept=[application/json, application/+json]
10:08:19.793 [main] DEBUG org.springframework.web.client.RestTemplate - Response 200 OK
10:08:19.814 [main] DEBUG org.springframework.web.client.RestTemplate - Reading to [java.util.List<com.spring.boot.maven.vue.rest.ClientRest>]
Exception in thread "main" org.springframework.web.client.RestClientException: Error while extracting response for type [java.util.List<com.spring.boot.maven.vue.rest.ClientRest>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.spring.boot.maven.vue.rest.ClientRest>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.spring.boot.maven.vue.course.Proposicao>` out of START_OBJECT token


at [Source: (PushbackInputStream); line: 1, column: 1]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:119)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1001)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:984)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:677)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:615)

Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.spring.boot.maven.vue.rest.ClientRest>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.spring.boot.maven.vue.rest.ClientRest>` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:245)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:104)
    ... 6 more
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.spring.boot.maven.vue.rest.ClientRest>` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1442)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1216)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1168)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:332)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:265)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3258)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:239)
    ... 8 more
  • Your mapping is wrong. In your Java class, you need an object Dados, that inside him will have a property totalCount and a list of an object DadosReais, that in it, at last, will have the properties id etc. In your call using RestTemplate, you don’t expect a list of ClientRest, but a single object ClientRest.

  • Got it. Thanks. I’ll try here...

1 answer

0


You need to look at JSON as the representation of an object, not as a set of Strings. The error is because Spring (in this case Jackson) cannot find an object that matches exactly what he expects.

Your mapping should be something like this:

public class ClientRest {
    private Dados dados;

    //getters e setters
}

public class Dados {

    private int totalCount;
    private List<DadosReais> dadosReais;

    //getters e setters

}

public class DadosReais {

    private Long id;
    private LocalDate anoApresentacao;
    private String txtDescricao;

    //getters e setters
}

After that, it’s enough:

ClienteRest response = restTemplate.getForObject("sua-url-aqui", ClienteRest.class);

Browser other questions tagged

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