Reputation: 16194
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!
Upvotes: 161
Views: 257099
Reputation: 538
To 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);
Upvotes: 7
Reputation: 744
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.
Upvotes: 1
Reputation: 534
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
Upvotes: 7
Reputation: 941
If you are using Spring Boot, you can set the following property in application.properties file.
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false
Upvotes: 94
Reputation: 538
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;
}
Upvotes: 2
Reputation: 142
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.
}
Upvotes: -2
Reputation: 91
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();
}
Upvotes: 7
Reputation: 527
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
Upvotes: 29
Reputation: 2692
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));
Upvotes: 1
Reputation: 456
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.
Upvotes: 1
Reputation: 846
If you use org.codehaus.jackson.map.ObjectMapper, then pls. use the following lines
mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
Upvotes: 3
Reputation: 1926
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).
Upvotes: 181
Reputation: 4385
In my case, I missed to write @JsonProperty annotation in one of the fields which was causing this error.
Upvotes: 3
Reputation: 2383
You can also probably annotate the class with @JsonIgnoreProperties(ignoreUnknown=true)
to ignore the fields undefined in the class
Upvotes: 11
Reputation: 629
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.
Upvotes: 27