44

Is there a global configuration in spring that can disable spring FAIL_ON_EMPTY_BEANS for all controller annotated with @ResponseBody?

user2412555
  • 1,055
  • 1
  • 16
  • 32

5 Answers5

83

If you are using Spring Boot, you can set the following property in application.properties file.

spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

Thanks to @DKroot for his valuable comment. But I believe this should be its own answer for others.

user238607
  • 1,580
  • 3
  • 13
  • 18
  • 5
    Good tip, but beware it wont' work with HAL, as Spring HATEOAS constructs its own object mapper. https://github.com/spring-projects/spring-hateoas/issues/333 – Jannik Sep 15 '17 at 11:38
36

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" >
vtor
  • 8,989
  • 7
  • 51
  • 67
9

cant find spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false in spring boot 2.2.5

I use this

@Configuration
public class SerializationConfiguration {
    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }
}
Evol Rof
  • 2,528
  • 2
  • 22
  • 37
  • 1
    Worked like a charm.. Thank you, propertie `spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false` never worked for me even with spring finding it – Renan Ribeiro Dec 22 '21 at 15:34
3

If you are using Spring Boot / JPA , you also have to observe if you are using

getOne (goes for jpa getReference ) for the findOne / enetiyManager.find(Clazz , id)

GetOne Relies on Persistence cached reference by ID that is designed to retrieve entity with only ID in it. Its use was mostly for indicating reference existed without the need to retrieve whole entity.

The find method is straight forward to persistence manager to obtain the persistent instance.

This second one will observe you annotation @JsonIgnore accordingly and will give you the expected result.

 // On entity...
 @JsonIgnore
 @OneToMany(fetch = FetchType.LAZY, mappedBy = "foo")
 private List<Foo> fooCollection;

 // later on persistence impl
 entityManager.find(Caso.class, id);

 // or on serivce 
 casoRepository.findById(id); //...
wdavilaneto
  • 788
  • 6
  • 7
0

For me, the Issue was with typecasting from org.json.JSONObject object to org.json.simple.JSONObject and I solved it by parsing value from org.json.JSONObject and then cast it to use as org.json.simple.JSONObject

JSONParser parser = new JSONParser();
org.json.simple.JSONObject xmlNodeObj = (org.json.simple.JSONObject) parser.parse(XMLRESPONSE.getJSONObject("xmlNode").toString());