How to Disable Fail_On_Empty_Beans in Jackson

How do I disable fail_on_empty_beans in Jackson?

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

Spring and jackson, how to disable FAIL_ON_EMPTY_BEANS through @ResponseBody

You can configure your object mapper when configuring configureMessageConverters

@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
MappingJackson2HttpMessageConverter converter =
new MappingJackson2HttpMessageConverter(mapper);
return converter;
}

If you want to know how to do exactly in your application, please update your question with your configuration files (xml or java configs).

Here is a good article how to customize message converters.

Edit: If you are using XML instead of Java configs, you can create a custom MyJsonMapper class extending ObjectMapper with custom configuration, and then use it as follows

public class MyJsonMapper extends ObjectMapper {    
public MyJsonMapper() {
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}
}

In your XML:

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>


<bean id="jacksonObjectMapper" class="com.mycompany.example.MyJsonMapper" >

(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

Th problem is you're trying to return Response.ok(imageFile) from your controller's uploadImage method. Spring then complains, because it doesn't know how to convert a MultipartFile into JSON.

Since ProductServiceUtils.uploadToLocalFileSystem() returns a ResponseEntity<String>, I believe what you had in mind was to return that value from the controller method, i.e.:

public class ProductServiceImpl implements ProductService {

public ResponseEntity<String>uploadToLocalFileSystem(MultipartFile imageFile) {
ResponseEntity<String> result = this.productServiceUtils.uploadToLocalFileSystem(imageFile);
this.fileDownloadUri = (this.productServiceUtils.getFileDownloadUri());
return result;
}
}

@RestController
@RequestMapping(path = "/api")
public class ProductController {

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<?> uploadImage(@RequestParam MultipartFile imageFile){
this.imageFile = imageFile;
return productService.uploadToLocalFileSystem(imageFile);
}
}

As a side note, you shouldn't be storing the state associated to the current invocation (fileDownloadUri, imageFile) inside singleton beans. This makes them non-thread-safe. Instead, design the API in such a way to include any relevant information in the return value.

spring.jackson.serialization.fail-on-empty-beans

You need your bean to be empty and without recognized annotations

Note that empty types that this feature has only effect on those "empty" beans that do not have any recognized annotations

So if you remove the annotations it'll failed

public class Wrapper {

}

Error will be as

Unrecognized field "wrapper" (class com.Wrapper ), not marked as ignorable (0 known properties: ])

Jackson relevant code applied when mark it as UnknownSerializer:

if (isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)) {
if (ser instanceof UnknownSerializer) {
return true;

Similar exception with nested empty class:

public class Wrapper {
private Wrapper2 wrapper2;
}


Related Topics



Leave a reply



Submit