Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The import com.fasterxml.jackson.databind.ObjectMapper cannot be resolved

I'm trying to create a JSON file and i'm having trouble while trying to use ObjectMapper class. heres my class code:

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;
    }
}

and there's my 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>`

I cannot import ObjectMapper class even with the jackson-databind dependency added at the pom.xml file. and the jars jackson-annotations-2.9.0.jar, jackson-core-2.9.5.jar, jackson-databind-2.9.0.jar are at the classpath.

like image 238
Felipe Bedê Avatar asked May 08 '18 15:05

Felipe Bedê


People also ask

What does Jackson ObjectMapper do?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.

Is ObjectMapper Jackson thread-safe?

Mapper instances are fully thread-safe provided that ALL configuration of the instance occurs before ANY read or write calls.

What is JSON processing exception?

Class JsonProcessingExceptionIntermediate base class for all problems encountered when processing (parsing, generating) JSON content that are not pure I/O problems. Regular IOException s will be passed through as is. Sub-class of IOException for convenience.


2 Answers

remove the version for jackson dependency jackson-databind i.e. just write

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

in your pom.xml. And let spring-boot decide the version for you.

Feel free to remove the jars from classpath.

like image 146
Vinay Prajapati Avatar answered Sep 28 '22 07:09

Vinay Prajapati


Remove

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

Also take a look into the effective pom by right clicking pom.xml if you are on intellij Maven > Show Effective POM . Check for conflicts there.

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

and then try

 mvn dependency:purge-local-repository;

This will first resolve the entire dependency tree, then delete the contents from the local repository, and then re-resolve the dependencies from the remote repository.

like image 35
ahumblenerd Avatar answered Sep 28 '22 07:09

ahumblenerd