我添加了具有这种依赖关系的库
<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);
}
示例图片:
我在类路径中有这个库。 我尝试使用其他版本的杰克逊。当我使用版本2. +时,我遇到另一个疯狂的错误提示Idea只能在此类中找不到ObjectMapper类。
在这张照片上,我有未找到类(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>
答案 0 :(得分:1)
尝试使用com.fasterxml jackson-annotations和jackson-core程序包,通常最好使用2+版本。好像您拥有的类JsonUnwrapped来自org.codehaus.jackson包中的版本1,而其他一些库则使用版本2(具有不同的包com.fasterxml.jackson)。尝试将您的杰克逊依赖项替换为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputPath>R:\VisualStudioBuilds\$(AssemblyName)\bin\$(Configuration)\</OutputPath>
</PropertyGroup>
</Project>
至于缺少ObjectMapper,很可能您没有更新导入语句。因此,切换到版本2,并用fasterxml替换所有导入的Codehouse Jackson。所有杰克逊库都必须具有相同版本。
如果您的项目中有任何父项目,请检查是否在其中导入了jackson以及它的版本。它可能具有较旧的版本,在这种情况下,请分别更改您的库版本,或者只是删除该版本,以使其从父级继承。
有关更多详细信息,请参见https://dzone.com/articles/jackson-dependency-issue-in-spring-boot-with-maven
答案 1 :(得分:0)
我要添加杰克逊2.7.8版
<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>
比起在Maven Project中进行清洁和打包
<?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>PetClinicMain</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Pet Clinic</name>
<url>http://maven.apache.org</url>
<modules>
<module>se</module>
<module>clinic-pet-web</module>
</modules>
</project>
答案 2 :(得分:0)
我已经固定好了这个例外。我将杰克逊jar文件添加到WEB-INF / lib 正如我所理解的那样,添加依赖不够。 enter image description here