How do I disable fail_on_empty_beans in Jackson?

前端 未结 12 1240
广开言路
广开言路 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:17

    If you wish to get JSON object without any extra fields - please add this annotation to your class, it worked perfect for me.

    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    

    You can also add in your application.properties file this row, but it will add an extra field to your JSON.

    spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
    
    0 讨论(0)
  • 2020-12-01 06:17
    ObjectMapper mapper = new ObjectMapper();
    

    Hi,

    When I use mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)

    My json object values come '' blank in angular page mean in response

    Solved with the help of only below settings

    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker().
    withFieldVisibility(JsonAutoDetect.Visibility.ANY).withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                    .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                    .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
    
    0 讨论(0)
  • 2020-12-01 06:18

    In my case I didnt need to disable it , rather I had to put this code on top of my class : (and this solved my issue)

        @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)//this is what was added
        @Value //this was there already
        @Builder//this was there already
    
    public class NameOfClass {
         //some code in here.
    }
    
    0 讨论(0)
  • 2020-12-01 06:21

    You can do this per class or globally, I believe.

    For per class, try @JsonSerialize above class declaration.

    For a mapper, here's one example:

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    // do various things, perhaps:
    String someJsonString = mapper.writeValueAsString(someClassInstance);
    SomeClass someClassInstance = mapper.readValue(someJsonString, SomeClass.class)
    

    The StackOverflow link below also has an example for a Spring project.

    For REST with Jersey, I don't remember off the top off my head, but I believe it's similar.


    Couple of links I dug up: (edited 1st link due to Codehaus shutting down).

    • https://web.archive.org/web/20150513164332/https://jira.codehaus.org/browse/JACKSON-201
    • Jackson serializationConfig
    0 讨论(0)
  • 2020-12-01 06:26

    You can also probably annotate the class with @JsonIgnoreProperties(ignoreUnknown=true) to ignore the fields undefined in the class

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

    In my case, I missed to write @JsonProperty annotation in one of the fields which was causing this error.

    0 讨论(0)
提交回复
热议问题