4

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

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Where are you creating the ObjectMapper instance, in a java class file or in the spring config? – Richard Jun 06 '17 at 10:36
  • 1
    It is automatically created by `spring-boot` or `spring-mvc`, and should thereby use the configuration properties during initialization. – membersound Jun 06 '17 at 10:45
  • I think you need to change your tags to add spring-boot – Richard Jun 06 '17 at 10:47
  • In the URL you are passing name as query string. Query strings are case sensitive, where as URL domain name isn't. – harshavmb Jun 06 '17 at 11:05
  • That's exactly what the `MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES` feature is for: making query strings behave case insensitive. But somehow it does not work even though the feature exists. – membersound Jun 06 '17 at 11:06
  • Doest [this](https://stackoverflow.com/questions/38351964/jackson-ignoring-case-in-snake-case-keys) help you in any way? – harshavmb Jun 06 '17 at 11:22
  • No, it just states that the feature exists, which I'm aware of. – membersound Jun 06 '17 at 11:26
  • Even though I don't configure `application.properties file`, it also works well in my environment. I annotated `TestReq` class with @Data from `lombok`. – niaomingjian Jun 06 '17 at 11:28
  • Which version of spring are you using? I'm on `1.5.3`. – membersound Jun 06 '17 at 11:31
  • The version is v1.5.1.RELEASE. – niaomingjian Jun 06 '17 at 11:33
  • 1
    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 Jun 06 '17 at 11:38
  • @M.Deinum is there a way to achieve the same effect of `ACCEPT_CASE_INSENSITIVE_PROPERTIES` and `FAIL_ON_UNKNOWN_PROPERTIES` on URL parameters? – membersound Jun 06 '17 at 11:39
  • Not an easy one that I'm aware of. You could create a filter which would wrap the request and uses a `CaseInsensitiveMap` to store/retrieve the request params. You would have to do it yourself, as stated data binding doesn't do anything with Jackson. – M. Deinum Jun 06 '17 at 11:41
  • 1
    I'm able to replicate on `1.5.3.RELEASE` version. – harshavmb Jun 06 '17 at 11:44
  • @M.Deinum Indeed I tested `@PostMapping` with `@RequestBody` successfully – membersound Jun 06 '17 at 11:47
  • Did you tried `spring.jackson.mapper.ACCEPT_CASE_INSENSITIVE_PROPERTIES=true` ? – Sigrist Jun 08 '17 at 12:05

2 Answers2

3

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.

andyf
  • 3,262
  • 3
  • 23
  • 37
  • Current version of spring/spring-boot uses `spring.jackson.mapper.accept-case-insensitive-properties=true` (hyphens not underscores for the property name; not sure if case-sensitive or not). – Roddy of the Frozen Peas Jul 31 '21 at 18:10
1

Simple answer: it is not possible.

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

membersound
  • 81,582
  • 193
  • 585
  • 1,120