4

How I can set properties to DEFAULT_VIEW_INCLUSION in yml file in Spring?

Now, I have this

spring: jackson: mapper: DEFAULT_VIEW_INCLUSION: true

But doesn't work.

Anthon
  • 69,918
  • 32
  • 186
  • 246

3 Answers3

4

This is a somewhat older question, but the questioner asked about how to do it in YAML in Spring Boot (using the "application.yml" file), not how to override the Mapper. Since overriding the Mapper causes you to lose all the other auto-configure benefits, let's assume the mode of overriding was important to the questioner. So, I'll answer the YAML question.

The questioner's problem is the syntax. This is how the Spring Boot documentation says you set it in YAML:

spring:
  jackson:
    default-property-inclusion: non_null

Note the indentation, it's important. "spring" is up against the left margin, "Jackson" is one tab-stop indented, and then the property is another tab-stop indented. There must be a space between the ":" and the "non_null" property value or the parser doesn't see the property name correctly.

If you have other Spring properties to set, this section of your "application.yml" YAML file might look something like this:

spring:
  cloud:
    config:
      enabled: false
  jackson:
    default-property-inclusion: non_null

Cautionary Note
That said, I tried to use this setting in a project with Spring Boot Starter v2.0.3 as its Parent, to suppress Nulls from Spring HATEOAS' ResourceSupport class. It didn't suppress any nulls in the Response objects.

This is where the overriding suggestion can offer a clue as to why the setting isn't 'working'. In my case, because the project used Jersey with Spring Boot instead of Spring MVC, I discovered that, for some reason, the JerseyConfiguration was overriding Boot's auto-configured mapper, and it explicitly set the JsonInclude to ALWAYS - so no Spring Boot setting was going to solve it:

public class JerseyConfiguration extends ResourceConfig {

    @Autowired
    public JerseyConfiguration(ObjectMapper objectMapper) {
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);

        packages("<removed actual class packages>");
        register(ResponseFilter.class);
        register(new ObjectMapperContextResolver(objectMapper));
        configureSwagger();
    }

So, one answer said to override the Mapper to 'solve' the problem. But you might find that your problem is occurring because someone already overrode the Spring Boot Mapper and has set an explicit include policy already. That was the issue on my project and changing the policy in the Jersey Config fixed my issue.

CelticPoet
  • 588
  • 1
  • 5
  • 13
2

Try with Java Config:

@Configuration
public class JacksonMapperConfig {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
        return mapper;
    }
}
codependent
  • 23,193
  • 31
  • 166
  • 308
2

If you're using Spring Boot, you can autowire the ObjectMapper:

@Configuration
public class JacksonConfig {

  @Autowired
  private ObjectMapper objectMapper;

  @PostConstruct
  public void configureObjectMapper() {
      objectMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
  }

}
René Winkler
  • 6,508
  • 7
  • 42
  • 69