1
I am consuming a REST API with RestTemplate
and it does not follow good practices, such as using HTTP status codes, for example.
Response in case of success:
{
"435": {
"Codigo": "435",
"Tipo": "",
"Corretor": "62",
"Cliente": "48304",
"DataHora": "2016-04-27 14:18:24",
"DataHoraAtualizacao": "",
"Assunto": "Visita - Imóvel 2",
"Local": "",
"Texto": "",
"DataHoraInicio": "2016-04-28 12:00:00",
"DataHoraFinal": "2016-04-28 12:20:00",
"Particular": "Nao",
"Imovel": "2",
"DiaInteiro": "Nao",
"Tarefa": "Nao",
"Concluido": "Nao"
},
"687": {
"Codigo": "687",
"Tipo": "",
"Corretor": "20",
"Cliente": "33040",
"DataHora": "2016-07-18 17:09:28",
"DataHoraAtualizacao": "",
"Assunto": "Visita - Imóvel 2",
"Local": "",
"Texto": "teste",
"DataHoraInicio": "2016-07-28 08:00:00",
"DataHoraFinal": "2016-07-28 09:00:00",
"Particular": "Nao",
"Imovel": "2",
"DiaInteiro": "Nao",
"Tarefa": "Nao",
"Concluido": "Nao"
}}
So far it is possible to notice a Map structure, as follows:
Map<String, MeuObjeto> meusObjetos;
Class MeuObjeto
public class MeuObjeto {
@JsonProperty("Codigo")
private Integer codigo;
@JsonProperty("Tipo")
private String tipo;
@JsonProperty("Corretor")
private String corretor;
@JsonProperty("Cliente")
private String cliente;
@JsonProperty("DataHora")
private String dataHora;
@JsonProperty("DataHoraAtualizacao")
private String dataHoraAtualizacao;
@JsonProperty("Assunto")
private String assunto;
@JsonProperty("Local")
private String local;
@JsonProperty("Texto")
private String texto;
@JsonProperty("DataHoraInicio")
private String dataHoraInicio;
@JsonProperty("DataHoraFinal")
private String dataHoraFinal;
}
Answer in case of error (no records):
{
"status": "200",
"message": "A pesquisa não retornou resultados."
}
How I mapped the class to predict the two cases?
You can create a function segmentation for the class instance to generate response-based event outputs.
– Lollipop
Is not returned the status code in the response header? Or is it always return something like 200 and the status code "correct" is always in the payload?
– Bruno César
The vendor API is exactly like this, and does not use http status.
– Murillo Goulart
In this case I think you will need to create a class with the attributes
status
andmessage
and when you receive the answer test if the Map is null, if it is, it is because the answer must be error.– romarcio
Okay, but where will the map be in the class?
– Murillo Goulart
have you thought of a map list? ex:
List<Map<String, Object>> ou depende do seu caso, Map<String, List<Object>>
– Guilherme Oliveira