membersound
membersound

Reputation: 86627

How to customize Jackson ObjectMapper with Spring application.properties?

I want to enable the following jackson mapper feature: MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES

According to https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html:

Could be configured in application.properties as follows: spring.jackson.mapper.accept_case_insensitive_properties=true

But:

@RestController
public class MyServlet {
    @RequestMapping("/test")
    public void test(@Valid TestReq req) {

    }
}

public class TestReq {
    @NotBlank
    private String name;
}

Usage:

localhost:8080/test?name=test //works
localhost:8080/test?Name=test //fails with 'name may not be blank'

So, the case insensitive property is not taken into account. But why?

By the way: even using Jackson2ObjectMapperBuilderCustomizer explicit does not work:

@Bean
public Jackson2ObjectMapperBuilderCustomizer initJackson() {
    Jackson2ObjectMapperBuilderCustomizer c = new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
        }
    };

    return c;
}

spring-boot-1.5.3.RELEASE

Upvotes: 4

Views: 12564

Answers (2)

andyf
andyf

Reputation: 3340

According to spring doc you can customize it.

I fix this problem by set my application.yml like this(spring 2.0):

 spring:
  jackson:
    mapper:
      ACCEPT_CASE_INSENSITIVE_PROPERTIES: true

Did you tried change your setting accept_case_insensitive_properties to UPPER CASE?

Also you can keep output to Upper Case by setting like this:

  jackson:
    mapper:
      ACCEPT_CASE_INSENSITIVE_PROPERTIES: true
    property-naming-strategy: com.fasterxml.jackson.databind.PropertyNamingStrategy.PascalCaseStrategy

Note that PascalCaseStrategy was deprecated now, but still working.

Upvotes: 4

membersound
membersound

Reputation: 86627

Simple answer: it is not possible.

The Jackson2ObjectMapperBuilderCustomizer affects the JSON POST requests only. It has no effect on the get query binding.

Upvotes: 1

Related Questions