How to map REST request response

Asked

Viewed 204 times

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.

  • 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?

  • The vendor API is exactly like this, and does not use http status.

  • In this case I think you will need to create a class with the attributes status and message and when you receive the answer test if the Map is null, if it is, it is because the answer must be error.

  • Okay, but where will the map be in the class?

  • have you thought of a map list? ex: List<Map<String, Object>> ou depende do seu caso, Map<String, List<Object>>

Show 1 more comment

1 answer

-1

You could put the treatment for error response if there is an Exception when doing the response serialization.

 try {
    MeuObjeto mo = new MeuObjeto(...)//recebe o response do request
 } catch (Exception e) {
    ErroMsg em = new ErroMsg(...)
 }

Browser other questions tagged

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