【问题标题】:Dropwizard ObjectMapper not honouring enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)Dropwizard ObjectMapper 不支持启用(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
【发布时间】:2019-12-30 07:25:29
【问题描述】:

我希望杰克逊能够解析不区分大小写的枚举。例如

public enum OperType {
  SUM
  PRODUCT
}

我想在 POST 请求中同时接受“SUM”和“sum”。

我在 Application::run 中获取 objectMapper 并启用设置:

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

但这没有任何效果!

【问题讨论】:

    标签: java enums dropwizard jackson-databind


    【解决方案1】:

    Jersey 没有使用 Dropwizard 引导程序中的 objectMapper,尽管 Dropwizard 的官方文档可能会让人相信。 需要在 Application::run 中注册自定义 ContextResolver 以使其工作:

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

    地点:

    @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;
        }
    
    }
    

    这些关于 dropwizard 生态系统的文档对于那些还不太精通的人来说可能真的很困惑!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-10
      • 2018-09-23
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      相关资源
      最近更新 更多