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?
– Mateus
Put your block class, maybe this Deserializer is unnecessary.
– Leonardo Villela