How do I disable fail_on_empty_beans in Jackson?

JavaJackson

Java Problem Overview


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 thing in the world, but hell it is late and I haven't been able to find a simple tutorial or anything particularly obvious from the api. SerializationFactory? Why would they make it so unintuitive and then make the error message seem so straightforward?

Although I do like the error message, I mean, it is better than an NPE.

I'm assuming there is a way to do this using annotations - but I'm not keen on using them at all for the simplistic work I'm doing!

Java Solutions


Solution 1 - Java

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).

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

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

mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

Solution 9 - Java

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

Solution 10 - Java

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.

Solution 11 - Java

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

Solution 12 - Java

I don't fully understand the reason behind this exception, but for Spring Boot projects adding the following to the properties file works a treat

application.yml

spring:
  jackson:
   serialization:
     FAIL_ON_EMPTY_BEANS: false

application.properties

spring.jackson.serialization.FAIL_ON_EMPTY_BEANS = false

Solution 13 - Java

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

Solution 14 - Java

If it is a Spring App, just paste the code in config class

        @Bean
        public ObjectMapper getJacksonObjectMapper() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.findAndRegisterModules();
            objectMapper.configure(
                    com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper;
        }
  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbharalView Question on Stackoverflow
Solution 1 - JavaPancakeoView Answer on Stackoverflow
Solution 2 - Javaabhishek jainView Answer on Stackoverflow
Solution 3 - JavaRJStanfordView Answer on Stackoverflow
Solution 4 - Javaassaf.govView Answer on Stackoverflow
Solution 5 - JavaAllDayAmazingView Answer on Stackoverflow
Solution 6 - Javamephistopheles78View Answer on Stackoverflow
Solution 7 - JavaWahib Ul HaqView Answer on Stackoverflow
Solution 8 - JavathangamanikasiView Answer on Stackoverflow
Solution 9 - JavaR PiduguView Answer on Stackoverflow
Solution 10 - JavaKirk B.View Answer on Stackoverflow
Solution 11 - JavaShahid Hussain AbbasiView Answer on Stackoverflow
Solution 12 - JavaAussieDev81View Answer on Stackoverflow
Solution 13 - JavaAppEmmanuelView Answer on Stackoverflow
Solution 14 - JavaR PiduguView Answer on Stackoverflow