Search code examples
javaenumsdropwizardjackson-databind

Dropwizard ObjectMapper not honouring enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)


I want to be able for jackson to parse case insensitive enums. For e.g

public enum OperType {
  SUM
  PRODUCT
}

i want to accept both "SUM" and "sum" in the POST request.

I am getting hold of objectMapper in Application::run and enabling the setting:

environment.getObjectMapper().enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);

But this is having no effect!


Solution

  • Jersey doesn't use objectMapper from Dropwizard bootstrap despite what Dropwizard's official documentation might lead one to believe. Needed to register custom ContextResolver in the Application::run to make it work:

    environment.jersey().register(new ObjectMapperContextResolver(injector.getInstance(ObjectMapper.class)));
    

    where:

    @Provider
    public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
    
        private final ObjectMapper mapper;
    
        public ObjectMapperContextResolver(ObjectMapper mapper) {
            this.mapper = mapper;
        }
    
        @Override
        public ObjectMapper getContext(Class<?> type) {
            return mapper;
        }
    
    }
    

    Man these documentations around the dropwizard ecosystem can be really confusing for someone who isn't as well versed yet!