Como faço para trazer todos os dados de um serviço externo usando Spring Boot. Nunca fiz isso para trazer as informações. Já criei aplicações usando dados de um banco. Vi alguns tutoriais mas não obtive o resultado esperado.
O Serviço retorna estes dados:
{
"dados": {
"totalCount": 100,
"dadosReais": [{
"id": 0001,
"anoApresentacao": 1995,
"txtDescricao": "exemplo de descrição",
etc...
}]
}
}
Meu mapeamento:
public class ClientRest{
private String dados;
private String dadosReais;
private long id;
private Date dataApresentacao;
private String txtDescricao;
//getters e setters
Minha chamada GET para o serviço:
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());
}
}
Retorno:
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
Dados
, que dentro dele terá uma propriedadetotalCount
e uma lista de um objetoDadosReais
, que dentro dele, por fim, terá as propriedadesid
etc. Na sua chamada usandoRestTemplate
, você não espera uma lista deClientRest
, mas sim um único objetoClientRest
.