-1

this is literally making my head explode because I can´t find a pretty way to solve this: I'm working with complex responses like this.

{
"etiquetaPago": "1001462713         ",
"codigoAdhesion": "01",
"vencimientos": null,
"empresa": {
    "codigo": "911",
    "descripcion": "Universidad Arg. John F. Kennedy",
    "rubro": {
        "codigo": "14",
        "descripcion": "Instituciones Educativas"
    },
    "manejaBaseDeudas": false,
    "manejaMultiplesConceptos": false,
    "manejaIngresoReferencia": true,
    "manejaIngresoImportes": false,
    "habilitado": true,
    "importesFijos": null,
    "importesPosibles": null
},
"concepto": [
    {
        "codigoConcepto": "001",
        "descripcionConcepto": "Cuota",
        "tipoPago": "PAGO_IGUAL",
        "manejaIngresoReferencia": true,
        "textoIngresoReferencia": true,
        "validaLongitudReferencia": true,
        "longitudMinimaTextoReferencia": 2,
        "longitudMaximaTextoReferencia": 2,
        "manejaIngresoImportes": false,
        "manejaRango": true,
        "importeFijo": 100.0,
        "importeMinimo": 100.0,
        "importeMaximo": 100.0
     }
  ]
}

And I have to parse it into a DTO class like this:

public class ConsultaPagosAgendaResponseDTO {


    List<PagoAgendaDTO> pagosAgendas;


}

Which has inside:

public class PagoAgendaDTO {

    private String etiquetaPago;
    private String codigoAdhesion;
    private EmpresaDTO empresa;
    private List<VencimientoDTO> vencimientos;
    private List<ConceptoDTO> conceptos;

}

public class EmpresaDTO {

    private String codigo;
    private String descripcion;
    private RubroDTO rubro;
    private boolean manejaBaseDeudas;
    private boolean manejaMultiplesConceptos;
    private boolean manejaIngresoReferencia;
    private boolean manejaIngresoImportes;
    private boolean habilitado;
}

And goes on..

The easy way would be making it by hand, iterating with foreach, but I would like to go with other type of approach, but don't know any, what would you recommend?

In this case DTO may be exactly as response, but they may differ too.

Thanks

  • Might take a look at some of the responses to this question: https://stackoverflow.com/questions/1957406/generate-java-class-from-json . – xdhmoore May 03 '20 at 05:05

1 Answers1

0

It really depends on how you're going to use it and what you're going to achieve.

If you just want to take some values from there, you can deserialize it to a tree. I believe, you're using Jackson. So you can use ObjectMapper#readTree and you won't need to have such a big number of DTOs. But as a drawback, it'll be not quite convenient to manipulate with the response.

Other way, if you don't need all the values from response, you can have DTOs with only used fields, and use @JsonIgnoreProperties(ignoreUnknown = true) to ignore others.

And if you need all the data, like to make some business login upon it, then it'll be more convenient to have such kind of DTOs, even it seems boilerplate to write them.

Mikhail Kopylov
  • 2,008
  • 5
  • 27
  • 58