Spring / Jackson Json - Problem in deserialize

1

This question is a follow-up to another question, a user answered found the solution to my other question ( Spring Custom Json Receive ), but I'm now having trouble mapping using Jackson.

I want to receive a custom request in JSON and send a "default" (Spring) response 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
}

A Doubt

How to map the "Drawn" object which is an Embedded object of the Blocking class.

@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;
    }
}
    
asked by anonymous 01.07.2016 / 22:09

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;
}

}

    
02.07.2016 / 03:07