导入 com.fasterxml.jackson.databind.ObjectMapper 无法解析
Posted
技术标签:
【中文标题】导入 com.fasterxml.jackson.databind.ObjectMapper 无法解析【英文标题】:The import com.fasterxml.jackson.databind.ObjectMapper cannot be resolved 【发布时间】:2018-10-18 14:27:38 【问题描述】:我正在尝试创建一个 JSON 文件,但在尝试使用 ObjectMapper 类时遇到了问题。这是我的课程代码:
import java.math.BigDecimal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import desafiodimensiva.entities.Desafio2;
@RestController
public class Desafio2Controller
@RequestMapping(value = "/teste")
public String index()
return "Greetings from Spring Boot!";
@RequestMapping("/desafio2")
public Desafio2 novoDesafio2()
ObjectMapper mapper = new ObjectMapper();
Desafio2 pessoa = new Desafio2();
BigDecimal valorFinanceiroPessoa = new BigDecimal("0.00");
pessoa.setNomePessoa("----------- ");
pessoa.setIdPessoa("0000");
pessoa.setValorFinanceiro(valorFinanceiroPessoa);
return pessoa;
还有我的 pom.xml :
`<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>com.desafio.dimensiva</groupId>
<artifactId>desafiodimensiva</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Desafio Dimensiva</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat</artifactId>
<version>8.5.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5-pre3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<finalName>desafiodimensiva</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>`
即使在 pom.xml
文件中添加了 jackson-databind
依赖项,我也无法导入 ObjectMapper 类。并且 jars jackson-annotations-2.9.0.jar, jackson-core-2.9.5.jar, jackson-databind-2.9.0.jar
在类路径中。
【问题讨论】:
删除这个依赖...检查你的依赖树mvn dependency:tree
,你会发现多个...它带有spring boot starter web,尤其是你正在使用的版本!
试试mvn dependency:purge-local-repository
。这应该可以清理所有内容并可能解决问题。
【参考方案1】:
删除jackson依赖jackson-databind
的版本,即直接写
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
在您的pom.xml
中。让 spring-boot 为你决定版本。
随意从类路径中删除 jars。
【讨论】:
请提及 pom.xml 文件的位置【参考方案2】:删除
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version> <--- remove this
</dependency>
如果您在 intellij Maven > Show Effective POM 上,还可以通过右键单击 pom.xml 来查看有效的 pom。检查那里的冲突。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
然后试试
mvn dependency:purge-local-repository;
这将首先解析整个依赖树,然后从本地存储库中删除内容,然后从远程存储库中重新解析依赖项。
【讨论】:
以上是关于导入 com.fasterxml.jackson.databind.ObjectMapper 无法解析的主要内容,如果未能解决你的问题,请参考以下文章
org.codehaus.jackson 与 com.fasterxml.jackson.core
Azure + SpringBoot 'com/fasterxml/jackson/core/JsonFactory'
fasterxml jackson 的包冲突太垃圾太恶心了: Could not initialize class com.fasterxml.jackson.databind.Seriali...
com.fasterxml.jackson.databind.JsonSerializer 按键排除字段
com.fasterxml.jackson.databind.exc.MismatchedInputException: