2017-04-03 78 views
0

我嘗試使用fasterXML傑克遜這樣注入的ObjectMapper:WELD-001408:類型ObjectMapper不滿意的依賴與預選賽@default在

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

我做錯了什麼?

+0

ObjectMapper'可注入嗎?我不認爲它是用任何合格的CDI註釋註釋的。 –

+0

在這種情況下,有必要創建一個提供者方法? –

+0

說實話,我不知道這個案例,因爲我不知道這個庫的所有功能,但是當我查看ObjectMapper的源代碼時,它沒有任何合格的CDI註釋。 –

回答

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

} 
相關問題