6

In my spring starter project, I am trying to do API calls using the JPA. My pom.xml look like this:

4.0.0

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.5</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
When I try to run it is getting error as

java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/Module

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
Programmer
  • 657
  • 4
  • 9
  • 21

3 Answers3

8

spring-boot-starter-web already includes Jackson. You should not override the managed version, otherwise the versions can be different between different Jackson libraries, causing ClassNotFoundException.

Remove this from the pom:

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

and let the transitive dependency with the correct version be used.

Or when you add Jackson to your pom, just don't use any version number, since parent project dependency management already includes all jackson libraries with a consistent version.

helospark
  • 1,420
  • 9
  • 12
2

Try 2 options :

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

or

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

  • Thank you for your response. I have added the following to pom.xml: com.fasterxml.jackson.core jackson-core 2.4.3 com.fasterxml.jackson.core jackson-databind 2.4.3 – Programmer Nov 28 '17 at 07:52
  • Now the error is gone. But while running getting an error as "Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat". Can you please help me to resolve this – Programmer Nov 28 '17 at 07:54
  • @Programmer ,it's problem with starting server Tomcat.First attempt to try-stop & clean Tomcat,and start server....if it's not starting,then second attempt:refactor with config:@SpringBootApplication,@ComponentScan... – Vladimir Vivi Nov 28 '17 at 08:01
  • I have added @SpringBootApplication,@EnableAutoConfiguration,@Configuration,@ComponentScan({"controller","service"}) the above lines to my main class and tried. Still, the error is there. – Programmer Nov 28 '17 at 08:13
  • @Programmer , then try removing this @ComponentScan({"‌controller","service‌​"}) and refactor server – Vladimir Vivi Nov 28 '17 at 08:25
0

If you are using SpringBoot you should know that Jackson is added by default. so remove to avoid conflicting versions and it should work.

Lho Ben
  • 2,053
  • 18
  • 35