线程“main” java.lang.NoSuchMethodError 中的异常:com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

2022-08-31 16:51:46

我需要将json转换为pojo。我决定使用 jackson,并将 jackson-core-2.2.0.jar、jackson-databind-2.4.4.jar 和 jackson-annotations-2.1.2.jar添加到我的项目的类路径中。

我创建了以下类:Main

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;

public class Json {
private static String SRC= "";

  public static void main(String[] args) {

      AwardList awardList = null;
      ObjectMapper mapper = new ObjectMapper();

      try{
          awardList = (AwardList) mapper.readValue(new URL(SRC), AwardList.class);
      }catch (JsonGenerationException e){
          e.printStackTrace();
       } catch (JsonMappingException e){
          e.printStackTrace();
       } catch (IOException e){
          e.printStackTrace();
       }
       System.out.println(awardList);

  }
}

以及以下类:AwardList

public class AwardList {

    private Flights[] flights;
    private String[] connections;

    private SaverEconomy saverEconomy;
    private StandartEconomy standartEconomy;
    private SaverBusiness saverBusiness;
    private StandartFirst standartFirst;
    private SaverFirst saverFirst;


    public Flights[] getFlights() {
        return flights;
    }

    public void setFlights(Flights[] flights) {
        this.flights = flights;
    }

    public SaverEconomy getSaverEconomy() {
        return saverEconomy;
    }

    public void setSaverEconomy(SaverEconomy saverEconomy) {
        this.saverEconomy = saverEconomy;
    }

    public StandartEconomy getStandartEconomy() {
        return standartEconomy;
    }

    public void setStandartEconomy(StandartEconomy standartEconomy) {
        this.standartEconomy = standartEconomy;
    }

    public SaverBusiness getSaverBusiness() {
        return saverBusiness;
    }

    public void setSaverBusiness(SaverBusiness saverBusiness) {
        this.saverBusiness = saverBusiness;
    }

    public StandartFirst getStandartFirst() {
        return standartFirst;
    }

    public void setStandartFirst(StandartFirst standartFirst) {
        this.standartFirst = standartFirst;
    }

    public SaverFirst getSaverFirst() {
        return saverFirst;
    }

    public void setSaverFirst(SaverFirst saverFirst) {
        this.saverFirst = saverFirst;
    }

    public String[] getConnections() {
        return connections;
    }

    public void setConnections(String[] connections) {
        this.connections = connections;
    }
}

我想将json转换为pojo并将其保存在数据库中。我不断收到以下错误:

Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
  at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:457)
  at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:379)
  at Json.main(Json.java:72)

答案 1

我遇到了完全相同的问题。我正在使用Maven进行依赖关系管理,并且只像这样为jackson-databind模块添加了依赖关系

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>

然后我通过这样做解决了它。我显式地添加了它的传递依赖项,并在 pom.xml 文件中为每个依赖项提到了相同的 jackson.version,如此处所示

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>

答案 2

我带着类似的问题来到这里,在Google App Engine上。这是我如何修复它。

首先我跑了:

mvn dependency:tree

查找谁在使用旧版本。然后,我将其从令人反感的依赖项中排除,如下所示:

<dependency>
    <groupId>com.google.appengine.tools</groupId>
    <artifactId>appengine-gcs-client</artifactId>
    <version>0.6</version>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

接下来,我在我的pom中添加了新版本的依赖项.xml:

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

希望这能帮助其他在这里跌倒的人。


推荐