使用Spring Initializr构建Spring Boot 2.0.0报错,Jackson 2.9.4是否缺失ObjectMapper?
Hey there! Let's unpack this issue step by step—first off, let's clear up the big question: Jackson 2.9.4 does NOT lack the ObjectMapper class. That class is the core of Jackson's serialization/deserialization functionality, and it's absolutely included in the jackson-databind dependency (the main Jackson core package). So the problem isn't with Jackson itself—it's tied to dependency changes between Spring Boot 1.5.x and 2.0.x.
Why This Happens Between Spring Boot Versions
Let's break down the dependency differences:
- Spring Boot 1.5.10: The
spring-boot-starter-webstarter automatically pulls in all necessary Jackson dependencies (jackson-core,jackson-annotations,jackson-databind) as transitive dependencies. So when you create a project with Web, JPA, and MySQL, everything just works out of the box. - Spring Boot 2.0.0: While
spring-boot-starter-webstill should include those Jackson dependencies, there are a couple scenarios where they might go missing:- Accidental Dependency Exclusion: If your project (or a third-party dependency you're using) uses Maven's
<exclusions>tag to remove Jackson core packages, this would break the transitive dependency chain. - Dependency Resolution Conflicts: Rarely, but possible—when combining
spring-boot-starter-data-jpawithspring-boot-starter-web, a conflicting version of a dependency might override or exclude Jackson's core packages.
- Accidental Dependency Exclusion: If your project (or a third-party dependency you're using) uses Maven's
The "Specific Dependency" You Added
Chances are, the dependency you added was jackson-databind (the package that contains ObjectMapper). Adding it explicitly fixes the issue because it bypasses any broken transitive dependency chain. Here's what that dependency looks like:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.4</version> </dependency>
How to Prevent This in the Future
To get to the root cause and avoid similar issues:
- Run
mvn dependency:treein your project directory. This will show you the full dependency tree—look forjackson-databindand check if it's marked as excluded by any other dependency. - Verify that Spring Boot's Jackson auto-configuration is active. Spring Boot 2.0 uses
JacksonAutoConfigurationto create a defaultObjectMapperbean automatically—this only works if the core Jackson packages are present. You don't need to manually configureObjectMapperunless you have custom serialization/deserialization needs.
Final Takeaway
You didn't miss any necessary configuration for ObjectMapper, and Jackson 2.9.4 definitely includes it. The issue was simply that your Spring Boot 2.0 project wasn't pulling in the required Jackson core packages via transitive dependencies—adding jackson-databind explicitly resolved that gap.
内容的提问来源于stack exchange,提问作者Gene Knight

