【问题标题】:WELD-001408: Unsatisfied dependencies for type ObjectMapper with qualifiers @Default atWELD-001408:带有限定符 @Default 的 ObjectMapper 类型的依赖关系不满足
【发布时间】:2017-08-28 12:27:49
【问题描述】:

我尝试使用 fastXML jackson 注入 ObjectMapper,如下所示:

@Inject
private ObjectMapper objectMapper;

但是当我尝试部署war文件时出现下一个错误:

org.jboss.weld.exceptions.DeploymentException: WELD-001408: 带有限定符的类型 ObjectMapper 的依赖关系不满足 @Default

这是我的依赖项:

    <!-- the core, which includes Streaming API, shared low-level abstractions (but NOT data-binding) -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <!-- Just the annotations; use this dependency if you want to attach annotations
         to classes without connecting them to the code. -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <!-- databinding; ObjectMapper, JsonNode and related classes are here -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>
    <!-- jackson-dataformat-yaml: Support for reading and writing YAML-encoded data via Jackson abstractions -->
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>${jackson-2-version}</version>
    </dependency>

我做错了什么?

【问题讨论】:

  • ObjectMapper 可以注射吗?我认为它没有使用任何合格的 CDI 注释进行注释。
  • 这种情况下需要创建provider方法吗?
  • 老实说,我不知道这种情况,因为我不太了解库,但是当我查看 ObjectMapper 的源代码时,它没有任何合格的 CDI 注释.
  • 我找到了基于这个线程stackoverflow.com/questions/18872931/…的解决方案

标签: java-ee-7 wildfly-10 fasterxml jackson2 jackson-databind


【解决方案1】:

我假设您需要ObjectMapper 对其进行一些配置。为此,您可以使用:

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    private final ObjectMapper mapper;

    public ObjectMapperContextResolver() {
        mapper = new ObjectMapper();
        // Do some configuration here
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 2015-06-10
    • 2018-12-19
    • 2015-04-05
    • 2015-02-09
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多