Spring/Jackson Json - Problem in deserialize

Asked

Viewed 483 times

1

This question is a continuation of another question, a user answered found the solution to my other question (Custom Json Spring Receipt), but I’m now having trouble mapping using Jackson.

I want to receive a custom request in JSON and send a "default" (Spring) answer in JSON.

Example

Request

{
    "codigo":1234,
    "cedente":1,
    "contaBancaria":1,
    "sacado":{
        "nome":"Victor",
        "documentoIdentificacao":"0000000",
        "endereco":"Endereço..."
        }
} 

Response

{
  "id": 46,
  "codigo": 1234,
  "cedente": {
    "id": 1,
    //...
  },
  "contaBancaria": {
    "id": 1,
    //...
  },
  "sacado": null
}

The Doubt

How to map the object "Drawn" which is an object Embedded bloquette.

@Embeddable
public class Sacado {
    private String nome;
    private String documentoIdentificacao;
    private String endereco;
    //Getters e Setters
}

Deserializer (currently)

public class BloqueteDeserialize extends JsonDeserializer<Bloquete> {
    @Override
    public Bloquete deserialize(JsonParser jp, DeserializationContext arg1)
        throws IOException, JsonProcessingException {
    JsonNode node = jp.readValueAsTree();
    Bloquete bloquete = new Bloquete();

    JsonNode nodeCodigo = node.get("codigo");
    if(nodeCodigo != null){
        bloquete.setCodigo(nodeCodigo.asLong());
    }
    JsonNode nodeCedente = node.get("cedente");
    if (nodeCedente != null) {
        bloquete.setCedente(new Cedente());
        long idCedente = nodeCedente.asLong();
        bloquete.getCedente().setId(idCedente);
    }
    JsonNode nodeContaBancaria = node.get("contaBancaria");
    if (nodeContaBancaria != null) {
        bloquete.setContaBancaria(new ContaBancaria());
        long idContaBancaria = nodeContaBancaria.asLong();
        bloquete.getContaBancaria().setId(idContaBancaria);
    }
    JsonNode nodeSacado = node.get("sacado");
    if (nodeSacado != null){
    }


    return bloquete;
    }
}
  • It has to be mandatorily using Jackson?

  • Put your block class, maybe this Deserializer is unnecessary.

1 answer

1


Hello your Deserialize would look like this:

public class BloqueteDeserialize extends JsonDeserializer<Bloquete> {
@Override
public Bloquete deserialize(JsonParser jp, DeserializationContext arg1)
        throws IOException, JsonProcessingException {
    JsonNode node = jp.readValueAsTree();
    Bloquete bloquete = new Bloquete();

    JsonNode nodeCodigo = node.get("codigo");
    if(nodeCodigo != null){
        bloquete.setCodigo(nodeCodigo.asLong());
    }
    JsonNode nodeCedente = node.get("cedente");
    if (nodeCedente != null) {
        bloquete.setCedente(new Cedente());
        long idCedente = nodeCedente.asLong();
        bloquete.getCedente().setId(idCedente);
    }
    JsonNode nodeContaBancaria = node.get("contaBancaria");
    if (nodeContaBancaria != null) {
        bloquete.setContaBancaria(new ContaBancaria());
        long idContaBancaria = nodeContaBancaria.asLong();
        bloquete.getContaBancaria().setId(idContaBancaria);
    }

    //Parte alterada do seu código.
    JsonNode nodeSacado = node.get("sacado");
    if (nodeSacado != null){
        Sacado sacado = new Sacado();
        sacado.setNome(nodeSacado.get("nome").asText());
        sacado.setDocumentoIdentificacao(nodeSacado.get("documentoIdentificacao").asText());
        sacado.setEndereco(nodeSacado.get("endereco").asText());

        bloquete.setSacado(sacado);

    }

    return bloquete;
}

}

  • Is there any "automated" way to do this? Or just this way taking each attribute of the class and setting it in the Node?

  • Hello, there is another way you can try as well: Sacado sacado = new ObjectMapper().treeToValue(nodeSacado, Sacado.class)

  • If it was useful for you the answer do not forget to mark the answer as useful and as the best response, good studies :)

  • 1

    It worked perfectly. Thank you! :)

Browser other questions tagged

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