java 无法解析导入Jackson

1tuwyuhd  于 2023-10-14  发布在  Java
关注(0)|答案(3)|浏览(140)

我试图创建一个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类。而jar jackson-annotations-2.9.0.jar, jackson-core-2.9.5.jar, jackson-databind-2.9.0.jar位于类路径中。

w6lpcovy

w6lpcovy1#

去除

<dependency>                                         
    <groupId>com.fasterxml.jackson.core</groupId>    
    <artifactId>jackson-databind</artifactId>        
    <version>2.9.0</version> <--- remove this
 </dependency>

如果您在intellijMaven > Show Effective POM上,还可以通过右键单击pom.xml来查看有效的POM。检查那里的冲突。

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

然后尝试

mvn dependency:purge-local-repository;

这将首先解析整个依赖关系树,然后从本地存储库中删除内容,然后从远程存储库中重新解析依赖关系。

cyej8jka

cyej8jka2#

上述解决方案对我不起作用。如果你仍然面临这个问题,那么另一种解决方案是添加以下依赖项-

<dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>${jackson.datatype.jsr310.version}</version>
</dependency>

如果这个解决方案对你有帮助的话,请你投赞成票。:)

e4eetjau

e4eetjau3#

删除Jackson依赖jackson-databind的版本,即只是写

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

你的pom.xml让spring-boot为您决定版本。
您可以随意从classpath中删除jar。

相关问题