通过maven

时间:2015-07-09 13:15:59

标签: java json maven intellij-idea dependencies

我在让maven下载我的应用程序所依赖的一些.jar文件方面遇到了一些麻烦。需要这些依赖关系的代码如下:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ServerConfiguration {
    public String info = null;
    public String idlURL = null;
    public String idlContents = null;
    public List<ServerInfo> servers = new ArrayList<>();

    public final void clear() {
        info = null;
        idlURL = null;
        idlContents = null;
        if (servers != null)
            servers.clear();
    }

    private final static ObjectReader jsonReader;
    private final static ObjectWriter jsonWriter;

    static {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); // <== Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator class file for com.fasterxml.jackson.core.JsonGenerator not found
        //mapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
        jsonWriter = mapper.writer();
        jsonReader = mapper.reader(ServerConfiguration.class);
    }

    public static ServerConfiguration fromJson(String json) throws IOException {
        return jsonReader.<ServerConfiguration>readValue(json); // <== Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException class file for com.fasterxml.jackson.core.JsonProcessingException not found
    }

    public String toJson() throws IOException {
        return jsonWriter.writeValueAsString(this);
    }

}

在阅读this问题之后,我尝试将提到的软件包( jackson-databind jackson-core )添加到 pom.xml

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>17.0</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.21.Final</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>4.2.2</version>
    </dependency>
</dependencies>

如何添加相应的依赖项?

编辑#1:

给出的错误如下(它们出现的行在上面的代码中标出):

Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator
  class file for com.fasterxml.jackson.core.JsonGenerator not found
Error:(54, 28) java: cannot access com.fasterxml.jackson.core.ObjectCodec
  class file for com.fasterxml.jackson.core.ObjectCodec not found
Error:(55, 28) java: cannot access com.fasterxml.jackson.core.Base64Variant
  class file for com.fasterxml.jackson.core.Base64Variant not found
Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException
  class file for com.fasterxml.jackson.core.JsonProcessingException not found
Error:(63, 26) java: cannot access com.fasterxml.jackson.core.Versioned
  class file for com.fasterxml.jackson.core.Versioned not found

编辑#2:

我似乎无法添加依赖项:

dependencies

2 个答案:

答案 0 :(得分:1)

对于数据绑定,您需要这个:

<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 <version>2.3.3</version>
</dependency>

缺少什么?您可以发布错误消息或堆栈跟踪吗?

Here is a good site to find dependencies for maven.

答案 1 :(得分:1)

Can you try with 2.5.4 version as below:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.4</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.4</version>
        </dependency>

In IntelliJ, try to tick a checkbox "export" in your dependencies.

I mean: Project Structure -> Modules -> Dependencies, and there you can see libs included to the module. You should also see a checkbox near each lib in column 'Export'.