Skip to lesson

Adding Jackson with Maven

A new project must read and write JSON, and a tutorial copied from the internet often will not compile. Mixed packages and versions are the usual cause, so setting Jackson up correctly once saves hours.

jackson-databind and the two libraries it brings with it

jackson-databind is the Jackson library you add to a project. It binds JSON text to Java classes, and it is built on jackson-core and jackson-annotations, which Maven adds for you.

One dependency brings three JARs. The program below prints the Jackson version and the JAR each class comes from.

Example 1 · JacksonJarsDemo.java: one dependency, three JARs

Runs in: the json-playground Maven project with Jackson 3.1.5

JacksonJarsDemo.javaJava
import com.fasterxml.jackson.annotation.JsonProperty;import java.nio.file.Path;import tools.jackson.core.JsonParser;import tools.jackson.databind.json.JsonMapper;
public class JacksonJarsDemo {
    // The name of the JAR file a class was loaded from    static String jarOf(Class<?> type) {        try {            Path jar = Path.of(type.getProtectionDomain().getCodeSource().getLocation().toURI());            return jar.getFileName().toString();        } catch (Exception e) {            return "unknown (" + e.getMessage() + ")";        }    }
    public static void main(String[] args) {        JsonMapper mapper = JsonMapper.builder().build();        System.out.println("Jackson version: " + mapper.version());        System.out.println("JsonMapper   -> " + jarOf(JsonMapper.class));        System.out.println("JsonParser   -> " + jarOf(JsonParser.class));        System.out.println("JsonProperty -> " + jarOf(JsonProperty.class));    }}
Output
Jackson version: 3.1.5
JsonMapper   -> jackson-databind-3.1.5.jar
JsonParser   -> jackson-core-3.1.5.jar
JsonProperty -> jackson-annotations-2.21.jar

What this code does

  • mapper.version() reports the Jackson version on the classpath, 3.1.5.
  • Each class comes from a different JAR: JsonMapper from jackson-databind, the parser from jackson-core and @JsonProperty from jackson-annotations. You declared only the first one.
  • jackson-annotations is version 2.21: Jackson 3 keeps using the 2.x annotations, which is why their package still starts with com.fasterxml.

Maven downloads the two smaller libraries as transitive dependencies (dependencies of your dependency):

  • jackson-core: the low-level parser and generator that reads and writes JSON tokens such as {, "sku" and 17999.
  • jackson-annotations: annotations such as @JsonProperty and @JsonIgnore that you put on your classes to control the JSON.

You write code against jackson-databind; the other two do the work underneath. This module uses Jackson 3.1.5, the version Spring Boot 4.1 manages.

Every example in this module is one file in the src/main/java folder of a small Maven project called json-playground. Small helper classes sit inside the demo class as static nested classes, so all the examples can live in the same project without name clashes.

Jackson 3 (tools.jackson) versus Jackson 2 (com.fasterxml)

Jackson 3 / 2 are the two major versions of the library. Jackson 3 uses the tools.jackson packages and unchecked exceptions; Jackson 2 uses com.fasterxml.jackson and checked exceptions.

Older projects use Jackson 2. The program below does the same job as the Jackson 3 one later in this lesson; only the imports, the mapper creation and throws differ.

Example 2 · FirstJsonDemo.java in Jackson 2, as older projects write it

Runs in: a Maven project with Jackson 2 (com.fasterxml.jackson.core:jackson-databind 2.21.5)

FirstJsonDemo.javaJava
import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;
// Jackson 2: the code you meet in older projects (Spring Boot 3 and earlier)public class FirstJsonDemo {
    public static void main(String[] args) throws JsonProcessingException {        ObjectMapper mapper = new ObjectMapper();
        List<String> categories = List.of("Mobiles", "Laptops", "Books");        System.out.println(mapper.writeValueAsString(categories));
        Map<String, Object> product = new LinkedHashMap<>();        product.put("sku", "BK-CLEAN");        product.put("name", "Clean Code");        product.put("stock", 30);        product.put("active", true);        String json = mapper.writeValueAsString(product);        System.out.println(json);
        Map<?, ?> back = mapper.readValue(json, Map.class);        System.out.println("Name read back: " + back.get("name"));        System.out.println("Stock read back: " + back.get("stock"));    }}
Output
["Mobiles","Laptops","Books"]
{"sku":"BK-CLEAN","name":"Clean Code","stock":30,"active":true}
Name read back: Clean Code
Stock read back: 30

What this code does

  • The imports start with com.fasterxml.jackson, the mapper is new ObjectMapper(), and main must declare throws JsonProcessingException because Jackson 2's exceptions are checked.
  • Compare it with Example 4, the same program in Jackson 3: the output is identical, only the imports, the mapper creation and the throws change.

Jackson 3 (released in 2025) moved to the Java packages tools.jackson.* and the Maven group tools.jackson.core. Jackson 2 used com.fasterxml.jackson.*. Spring Boot 4 uses Jackson 3; Spring Boot 3 and older projects use Jackson 2, so you will read both. The imports tell them apart, and so do these details:

  • Packages: tools.jackson.databind.json.JsonMapper (3) versus com.fasterxml.jackson.databind.ObjectMapper (2). The annotations stay in com.fasterxml.jackson.annotation in both.
  • Exceptions: Jackson 3 throws the unchecked JacksonException; Jackson 2 throws the checked JsonProcessingException, so its code is full of throws and try/catch.
  • Mapper: Jackson 3 mappers are built once with JsonMapper.builder() and never change afterwards; Jackson 2 code often calls setters such as mapper.configure(...) on a new ObjectMapper().
  • Dates: Jackson 3 handles LocalDate and other java.time types out of the box; Jackson 2 needs the extra jackson-datatype-jsr310 module.
Some defaults changed too, such as how unknown JSON fields and dates are handled. The lessons that cover those topics point out the Jackson 2 behaviour.

Adding the dependency and checking it with mvn dependency:tree

Transitive dependency is a library that one of your dependencies needs. Maven adds it for you, and mvn dependency:tree shows it nested under the library that brought it.

Adding Jackson is one <dependency> block in pom.xml. After that, this command lists every library your project uses.

Terminal
mvn dependency:tree

Example 3 · pom.xml with Jackson 3

Runs in: the json-playground Maven project

XML
<?xml version="1.0" encoding="UTF-8"?><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 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.shoplite</groupId>    <artifactId>json-playground</artifactId>    <version>1.0.0</version>    <properties>        <maven.compiler.release>17</maven.compiler.release>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <!-- Jackson 3: the same version Spring Boot 4.1 uses -->        <dependency>            <groupId>tools.jackson.core</groupId>            <artifactId>jackson-databind</artifactId>            <version>3.1.5</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.15.0</version>            </plugin>            <!-- mvn -q compile exec:java -Dexec.mainClass=FirstJsonDemo runs one example -->            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>exec-maven-plugin</artifactId>                <version>3.6.4</version>            </plugin>        </plugins>    </build></project>
Sample output of mvn dependency:tree
[INFO] --- dependency:3.6.1:tree (default-cli) @ json-playground ---
[INFO] com.shoplite:json-playground:jar:1.0.0
[INFO] \- tools.jackson.core:jackson-databind:jar:3.1.5:compile
[INFO]    +- com.fasterxml.jackson.core:jackson-annotations:jar:2.21:compile
[INFO]    \- tools.jackson.core:jackson-core:jar:3.1.5:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.442 s
[INFO] Finished at: 2026-10-16T08:11:07+05:30

What this code does

  • The one <dependency> gives three lines in the tree: jackson-databind at the top and, indented under it, the two libraries it brought in.
  • compile at the end of each line is the scope: the JAR is used to compile and to run the program. The time and date lines differ on every run.

The block has the group tools.jackson.core, the artifact jackson-databind and the version 3.1.5. The next build downloads the JARs into your local repository, the .m2 folder in your user folder.

Run the command after adding a dependency, and check that the versions are the ones you expect.

A first JsonMapper call to prove the setup works

JsonMapper is Jackson 3's main class for JSON. writeValueAsString(object) turns a Java value into JSON text, and readValue(json, type) turns JSON text back into a Java value.

Run one demo class from the project folder with the exec plugin. -q (quiet) hides Maven's own log lines, so you see only the program's output.

Terminal
mvn -q compile exec:java -Dexec.mainClass=FirstJsonDemo

Example 4 · FirstJsonDemo.java: Java to JSON and back with Jackson 3

Runs in: the json-playground Maven project with Jackson 3.1.5

FirstJsonDemo.javaJava
import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import tools.jackson.databind.json.JsonMapper;
public class FirstJsonDemo {
    public static void main(String[] args) {        JsonMapper mapper = JsonMapper.builder().build();
        // Java -> JSON        List<String> categories = List.of("Mobiles", "Laptops", "Books");        System.out.println(mapper.writeValueAsString(categories));
        Map<String, Object> product = new LinkedHashMap<>();        product.put("sku", "BK-CLEAN");        product.put("name", "Clean Code");        product.put("stock", 30);        product.put("active", true);        String json = mapper.writeValueAsString(product);        System.out.println(json);
        // JSON -> Java        Map<?, ?> back = mapper.readValue(json, Map.class);        System.out.println("Name read back: " + back.get("name"));        System.out.println("Stock read back: " + back.get("stock"));    }}
Output
["Mobiles","Laptops","Books"]
{"sku":"BK-CLEAN","name":"Clean Code","stock":30,"active":true}
Name read back: Clean Code
Stock read back: 30

What this code does

  • A List becomes a JSON array and a Map becomes a JSON object. LinkedHashMap keeps the keys in insertion order, so the JSON is printed in the same order.
  • readValue(json, Map.class) reads the text back into a Map; 30 comes back as an Integer. No throws is needed, because Jackson 3 exceptions are unchecked.

A program that writes and then reads JSON is the fastest proof that Maven, the imports and the JARs all line up.

Order of execution

Put the setup steps in order

Arrange the steps that take a new Maven project to its first JSON output.

Fixing 'package does not exist' and version clashes

BOM (bill of materials) is a special POM that fixes one version for a whole family of libraries. Importing the Jackson BOM keeps every Jackson part on the same version.

A Jackson 2 import in a Jackson 3 project will not compile, and mixed versions fail at run time. The two examples show the error and the BOM fix.

Example 5 · OldTutorialDemo.java: Jackson 2 imports in a Jackson 3 project

Runs in: the json-playground Maven project, with OldTutorialDemo.java added

Terminal
mvn -q compile
Sample output
[ERROR] COMPILATION ERROR :
[ERROR] /C:/Users/asha/json-playground/src/main/java/OldTutorialDemo.java:[1,38] package com.fasterxml.jackson.databind does not exist
[ERROR] /C:/Users/asha/json-playground/src/main/java/OldTutorialDemo.java:[7,9] cannot find symbol
  symbol:   class ObjectMapper
  location: class OldTutorialDemo
[ERROR] /C:/Users/asha/json-playground/src/main/java/OldTutorialDemo.java:[7,35] cannot find symbol
  symbol:   class ObjectMapper
  location: class OldTutorialDemo
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.15.0:compile (default-compile) on project json-playground: Compilation failure: Compilation failure:

What this code does

  • OldTutorialDemo.java starts with import com.fasterxml.jackson.databind.ObjectMapper;, but the project has only Jackson 3. [1,38] means line 1, column 38 of that file.
  • The cannot find symbol errors on line 7 follow from the first one. Fix the import (tools.jackson.databind.json.JsonMapper) and the rest disappear.

Example 6 · pom.xml with the Jackson BOM

Runs in: the json-playground Maven project

XML
<?xml version="1.0" encoding="UTF-8"?><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 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.shoplite</groupId>    <artifactId>json-playground</artifactId>    <version>1.0.0</version>    <properties>        <maven.compiler.release>17</maven.compiler.release>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <!-- The Jackson BOM: one version for every Jackson part, direct or transitive -->    <dependencyManagement>        <dependencies>            <dependency>                <groupId>tools.jackson</groupId>                <artifactId>jackson-bom</artifactId>                <version>3.1.5</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>    <dependencies>        <dependency>            <groupId>tools.jackson.core</groupId>            <artifactId>jackson-databind</artifactId>            <!-- no <version>: it comes from the BOM -->        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.15.0</version>            </plugin>            <!-- mvn -q compile exec:java -Dexec.mainClass=FirstJsonDemo runs one example -->            <plugin>                <groupId>org.codehaus.mojo</groupId>                <artifactId>exec-maven-plugin</artifactId>                <version>3.6.4</version>            </plugin>        </plugins>    </build></project>
Sample output of mvn dependency:tree
[INFO] --- dependency:3.6.1:tree (default-cli) @ json-playground ---
[INFO] com.shoplite:json-playground:jar:1.0.0
[INFO] \- tools.jackson.core:jackson-databind:jar:3.1.5:compile
[INFO]    +- com.fasterxml.jackson.core:jackson-annotations:jar:2.21:compile
[INFO]    \- tools.jackson.core:jackson-core:jar:3.1.5:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.827 s
[INFO] Finished at: 2026-10-16T08:15:45+05:30

What this code does

  • The BOM is imported with <type>pom</type> and <scope>import</scope> inside <dependencyManagement>. The databind dependency below has no <version>: the BOM supplies 3.1.5.
  • <dependencyManagement> also controls versions that arrive transitively, so an old jackson-core pulled in by another library would be lifted to 3.1.5 as well. The Jackson 2 BOM is com.fasterxml.jackson:jackson-bom.

package ... does not exist is a compile error: the code imports a package that is not on the classpath. With Jackson it almost always means the imports and the dependency are from different major versions, such as com.fasterxml.jackson.databind imports (copied from an older tutorial) in a project that has Jackson 3. Change the imports to tools.jackson..., or the dependency to match the code; do not add both versions to hide the error.

A version clash happens when Jackson's parts end up with different versions, for example an old jackson-core 2.14.2 next to jackson-databind 2.21.5. It compiles, then fails at run time with NoSuchMethodError: 'void com.fasterxml.jackson.core.util.BufferRecycler.releaseToPool()', because the newer databind calls a method the old core does not have. The BOM is imported in <dependencyManagement> and fixes one version for every part, direct or transitive. Spring Boot projects get this for free, because Boot manages Jackson's version.

Next step

Check your understanding

10 quick questions