How do I disable fail_on_empty_beans in Jackson?

前端 未结 12 1241
广开言路
广开言路 2020-12-01 05:44

Using jackson 2.1, how do I disable the fail_on_empty beans that the error message seems to want me to disable?

I\'m assuming this is just the simplest

相关标签:
12条回答
  • 2020-12-01 06:27

    If you use org.codehaus.jackson.map.ObjectMapper, then pls. use the following lines

    mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
    
    0 讨论(0)
  • 2020-12-01 06:36

    T o fix this issue configure your JsonDataFormat class like below

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    

    which is almost equivalent to,

    mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
    
    0 讨论(0)
  • 2020-12-01 06:38

    Adding a solution here for a different problem, but one that manifests with the same error... Take care when constructing json on the fly (as api responses or whatever) to escape literal double quotes in your string members. You may be consuming your own malformed json.

    0 讨论(0)
  • 2020-12-01 06:39

    If you are using Spring Boot, you can set the following property in application.properties file. spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

    0 讨论(0)
  • 2020-12-01 06:40

    You can also get the same issue if your class doesn't contain any public methods/properties. I normally have dedicated DTOs for API requests and responses, declared public, but forgot in one case to make the methods public as well - which caused the "empty" bean in the first place.

    0 讨论(0)
  • 2020-12-01 06:43

    In Jersey Rest Services just use the JacksonFeatures annotation ...

    @JacksonFeatures(serializationDisable = {SerializationFeature.FAIL_ON_EMPTY_BEANS})
    public Response getSomething() {
        Object entity = doSomething();
        return Response.ok(entity).build();
    }
    
    0 讨论(0)
提交回复
热议问题