151

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!

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
bharal
  • 15,461
  • 36
  • 117
  • 195

15 Answers15

173

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

Community
  • 1
  • 1
Pancakeo
  • 1,846
  • 1
  • 12
  • 5
  • I used this one earlier today, has some more info, e.g. auto discovery. http://stackoverflow.com/questions/4362104/strange-jackson-exception-being-thrown-when-serializing-hibernate-object – Pancakeo Mar 07 '13 at 02:21
  • 1
    Can you give an example on how to use `@JsonSerialize` to disable `FAIL_ON_EMPTY_BEANS` on class declaration? – tuk Apr 01 '16 at 11:09
  • 2
    Been a few years :). This might help: http://stackoverflow.com/questions/12162413/jackson-use-jsonserialize-inclusion-non-null-except-for-one-class – Pancakeo Jul 09 '16 at 20:38
  • Or you can use the disable method of ObjectMapper class like "mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)" – R Pidugu Mar 12 '18 at 12:54
90

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

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
abhishek jain
  • 901
  • 6
  • 3
29

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
assaf.gov
  • 517
  • 6
  • 19
26

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.

RJStanford
  • 619
  • 6
  • 5
11

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

AllDayAmazing
  • 2,383
  • 1
  • 24
  • 25
7

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

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);
Coding world
  • 167
  • 8
  • 23
R Pidugu
  • 490
  • 5
  • 8
5

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
AussieDev81
  • 454
  • 5
  • 11
3

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

Wahib Ul Haq
  • 4,185
  • 3
  • 44
  • 41
3

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

mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
thangamanikasi
  • 846
  • 6
  • 19
2

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;
        }
  
R Pidugu
  • 490
  • 5
  • 8
1

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.

Kirk B.
  • 456
  • 2
  • 6
1
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));
Artemis
  • 2,553
  • 7
  • 21
  • 36
Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
1

I get the same error when I return InvoiceDTO as a response to the client. The problem was when I used the bean (Customer) in the DTO (InvoiceDTO). So, to solve I changed the bean (Customer) to DTO (CustomerDTO) to disable serialization and it is worked fine.

abdella
  • 490
  • 5
  • 11
-1

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.
}
AppEmmanuel
  • 144
  • 1
  • 6