0

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

@Inject
private ObjectMapper objectMapper;

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

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

这是我的依赖项:

    <!-- 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>

我做错了什么?

4

1 回答 1

0

我假设您需要对其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;
    }

}
于 2017-04-07T18:47:14.067 回答