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