gpt4 book ai didi

java.lang.ClassNotFoundException 怎么可能?

转载 作者:行者123 更新时间:2023-12-02 10:44:10 25 4
gpt4 key购买 nike

我添加了具有此依赖项的库

        <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>

然后当我尝试创建 pojo 类 Idea 告诉我它找不到类但类存在java.lang.ClassNotFoundException:org.codehaus.jackson.annotate.JsonUnwrapped

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.addHeader("Content-Type", "application/json; charset=utf-8");
StringBuilder builder = new StringBuilder();
String a = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"))){
while ((a = reader.readLine())!=null){
builder.append(a);
}
}
ObjectMapper objectMapper = new ObjectMapper();
UserPojo pojo = objectMapper.readValue(builder.toString(),UserPojo.class);
}

示例图片:

我在类路径中有这个库。我尝试使用 jackson 的另一个版本。当我使用版本 2.+ 时,我遇到了另一个疯狂的错误 Idea can't find class ObjectMapper only in this class。

在这张图片上我有类(ObjectMapper)未找到错误:

在这张图片上我没有错误:

但这两个类在一个包中。这是怎么可能的以及如何解决?

添加pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>PetClinic</groupId>
<artifactId>cpw</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>Clinic Pet Web</name>
<url>http://maven.apache.org</url>

<dependencies>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.5.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>PetClinic</groupId>
<artifactId>se</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<finalName>PetClinicWeb</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

最佳答案

尝试使用 com.fasterxml jackson-annotations 和 jackson-core 包,一般情况下最好使用 2+ 版本。似乎您拥有的 JsonUnwrapped 类来自版本 1,位于 org.codehaus.jackson 包中,而其他一些库使用版本 2(具有不同的包 - com.fasterxml.jackson)。尝试用以下内容替换您的 Jackson 依赖项:

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

至于缺少 ObjectMapper,您很可能没有更新导入语句。因此切换到版本 2 并用 fastxml 替换所有导入的 codehouse jackson。所有 Jackson 库拥有相同版本非常重要。

此外,如果您的项目有任何父项目,请检查该项目是否导入了 jackson 以及它的版本。它可能有旧版本,在这种情况下分别更改您的库版本,或者只是删除该版本,以便它将从父级继承。

参见https://dzone.com/articles/jackson-dependency-issue-in-spring-boot-with-maven了解更多详情

关于java.lang.ClassNotFoundException 怎么可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com