Search code examples
javajacksonspring-bootjson-deserialization

Include root object with jackson in springboot deserializer


How can I include the objeto root in my jackson deserializer with spring-boot?

i try to put in application.properties

spring.jackson.deserialization.UNWRAP_ROOT_VALUE=true

i try using one configurator

@Configuration
public class JacksonConfig {

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.featuresToEnable(DeserializationFeature.UNWRAP_ROOT_VALUE);
        builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"));
        builder.indentOutput(true);
        return builder;
    }

}

And i put in my classe the annotation

@JsonRootName("contato")
public class TbContato extends EntityBase {

but dont work i got this return:

{
  "cdContato": 12,
  "dtContato": "03/08/2015 16:04:43",
  "cdUsuario": null,
  "nmParte": "Fabio Ebner",
  "nmEmailParte": "[email protected]",
  "nmAssunto": "Assuntttoooo",
  "dsMensagem": "mensagem nessa porra aqui",
  "dtResposta": null,
  "dsResposta": null,
  "cdUsuarioResposta": null,
  "nmUsuarioResposta": null
}

without the root.


Solution

  • That's because you're serializing not deserializing. Try using

    spring.jackson.serialization.WRAP_ROOT_VALUE=true