【问题标题】:EJB ObjectMapperEJB 对象映射器
【发布时间】:2019-03-14 09:30:29
【问题描述】:

我之前参与过一个 Spring 项目,您可以在其中创建一个 ObjectMapper bean。

使用 bean 的优点是易于测试和单点配置。

我当前的项目涉及 JEE Weblogic 环境。我不能像在 Spring 中那样从 ObjectMapper 创建 bean,因为 ObjectMapper 具有 JEE 不喜欢的最终方法。 我试图扩展在构造函数中配置它的 ObjectMapper。

我找到了用于在其余接口中注册自定义序列化程序的 ContextResolver 解决方案,但我想要一个可重用、依赖注入、预配置的 ObjectMapper,因为我们还在应用程序的其他部分序列化对象。

有可能实现吗?

如果我错过了什么,这是一个糟糕的主意,我也想知道原因。

提前致以诚挚的问候和感谢,

约翰尼斯

【问题讨论】:

    标签: jakarta-ee dependency-injection jackson weblogic12c


    【解决方案1】:

    所以我找到了问题和解决方案。

    ObjectMapper 具有 EJB 不支持的最终方法。

    解决方案是使用 CDI 创建 ObjectMapper bean,并使用 @Inject 进行注入:

    @ApplicationScoped
    public class SerializerConfig {
    
        @Produces
        public ObjectMapper objectMapper() {
            return new ObjectMapper();
        }
    }
    

    现在 ObjectMapper 是可注入的:

    public class ObjectMapperUser {
    
        @Inject
        private ObjectMapper objectMapper;
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 2023-03-18
      • 2021-02-17
      • 1970-01-01
      • 2019-09-19
      • 2016-12-04
      相关资源
      最近更新 更多