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
Possible duplicate of How to consume an external API in Spring Boot
– Wictor Chaves
Your mapping is wrong. In your Java class, you need an object
Dados
, that inside him will have a propertytotalCount
and a list of an objectDadosReais
, that in it, at last, will have the propertiesid
etc. In your call usingRestTemplate
, you don’t expect a list ofClientRest
, but a single objectClientRest
.– StatelessDev
Got it. Thanks. I’ll try here...
– Regis Paiva Araujo