Json Custom Spring Receipt

0

Hello, guys. All right?

Currently on my system I'm mapping objects to Spring's default form, but I'd like to do a custom mapping:

Current driver

@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Bloquete> create(@RequestBody Bloquete obj) {
    Bloquete objAtualizado = repository.save(obj);
    return new ResponseEntity<Bloquete>(objAtualizado, HttpStatus.CREATED);
}

Current request

{
    "cedente":{"id":40,"nome":"Empresa","documentoIdentificacao":"06789233133"},
    "contaBancaria":{"id":14,"agencia":"0400","conta":"9424283"},
}

Request desired

{
    "cedente":40,
    "contaBancaria":14,
}

Current and desired response

{
    "id":17382173
    "cedente":{"id":40,"nome":"Empresa","documentoIdentificacao":"06789233133"},
    "contaBancaria":{"id":14,"agencia":"0400","conta":"9424283"},
}

Conclusion

The idea is that who is sending the request does not have to have the information of the object just its id but not doing so:

{
    "cedente":{"id":40},
    "contaBancaria":{"id":14},
}

Thank you in advance.

    
asked by anonymous 28.06.2016 / 00:38

1 answer

1

You can customize the Serializer. The following code is using the api Jackson Json :

import java.io.IOException;
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonSerializer;
    import com.fasterxml.jackson.databind.SerializerProvider;
    public class BloqueteSerializer extends JsonSerializer<Bloquete> {
      @Override
      public void serialize(Bloquete bloquete, JsonGenerator jG, SerializerProvider arg2) throws IOException,JsonProcessingException {
        jG.writeStartObject();
        jG.writeNumberField("cedente", bloquete.getCedente().getId());
        jG.writeNumberField("contaBancaria", bloquete.getContaBancaria().getId());
        jG.writeEndObject();
      }
    }

and deserialize

import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
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();
        bloquete.setCedente(new Cedente); 
        bloquete.getCedente.setId(node.get("cedente").asInt())
        bloquete.setContaBancaria(new ContaBancaria);
        bloquete.getcontaBancaria.setId(node.get("contaBancaria"));
        return bloquete;
    }
}    

and the entity adds annotations:

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonDeserialize(using = BloqueteDeserialize.class)
@JsonSerialize(using = BloqueteSerializer.class)
public class Bloquete {

}

If I did not want to change the json customization, I think I could create a request class, I do not know if it would be good practice to do this.

public class BloqueteRequest{
      private int cedente;
      private int contaBancaria;
  }

and in the method would fill this class

@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<?> create(@RequestBody Bloquete obj) {
      Bloquete objAtualizado = repository.save(obj);

      BloqueteRequest br = new BloqueteRequest();
      br.setCedente(objAtualizado.getCedente.getId());
      br.setContaBancaria(objAtualizado.getContaBancaria.getid());


      return new ResponseEntity<BloqueteRequest>(br, HttpStatus.CREATED);
  }
    
29.06.2016 / 21:17