4
votes

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

2
Where are you creating the ObjectMapper instance, in a java class file or in the spring config? - Richard
It is automatically created by spring-boot or spring-mvc, and should thereby use the configuration properties during initialization. - membersound
I think you need to change your tags to add spring-boot - Richard
What you are trying is data binding which does nothing with Jackson. You are passing URL parameters whereas the Jackson serializer/mapper will only be active for response bodies NOT parameters. - M. Deinum
I'm able to replicate on 1.5.3.RELEASE version. - harshavmb

2 Answers

3
votes

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.

1
votes

Simple answer: it is not possible.

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