Skip to content

problem4j/problem4j-jackson

Repository files navigation

Problem4J Jackson

Build Status Sonatype Sonatype License

Jackson 2.x and 3.x integration module for problem4j-core. Provides easy support for serializing and deserializing the Problem model using Jackson's ObjectMapper.

Project contains two submodules, problem4j-jackson2 for Jackson 2.x and problem4j-jackson3 for Jackson 3.x. Both modules have the similar API and functionality, but are compiled against different versions of Jackson (and by extension against different versions of Java). Choose the one that matches the version of Jackson you are using in your project.

Module Jackson Version Java Baseline
problem4j-jackson2 com.fasterxml.jackson.core:jackson-databind:2.x.y Java 8
problem4j-jackson3 tools.jackson.core:jackson-databind:3.x.y Java 17

Instead of releasing version 2.0, library was split into two modules, because jackson-3.x has different maven groupId so it's technically possible to have both versions included in the same project. Note that each module is versioned independently.

Table of Contents

Features

  • ✅ Seamless JSON serialization of Problem objects.
  • ✅ Accurate deserialization into immutable Problem instances.
  • ✅ Compatible with standard Jackson ObjectMapper.
  • ✅ Pluggable via Jackson's Module system.
  • ✅ Lightweight, with no external dependencies beyond Jackson and problem4j-core.
  • ✅ Support for both Jackson2 and Jackson3.

Example

For problem4j-jackson2 (Jackson 2.x):

import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper mapper = new ObjectMapper().registerModule(new ProblemModule());

Problem problem = Problem.builder().title("Bad Request").status(400).detail("not a valid json").build();

String json = mapper.writeValueAsString(problem);
Problem parsed = mapper.readValue(json, Problem.class);

Module is included in com.fasterxml.jackson.databind.Module for automatic service discovery. Registration can also be done with findAndRegisterModules() method or by adding a ProblemMixIn.

ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
ObjectMapper mapper = new ObjectMapper().addMixIn(Problem.class, ProblemMixIn.class);

For problem4j-jackson3 (Jackson 3.x):

import tools.jackson.databind.json.JsonMapper;

JsonMapper mapper = JsonMapper.builder().addModule(new ProblemJacksonModule()).build();

Problem problem = Problem.builder().title("Bad Request").status(400).detail("not a valid json").build();

String json = mapper.writeValueAsString(problem);
Problem parsed = mapper.readValue(json, Problem.class);

Module is included in tools.jackson.databind.JacksonModule for automatic service discovery. Registration can also be done with findAndAddModules() method or by adding a ProblemJacksonMixIn.

JsonMapper mapper = JsonMapper.builder().findAndAddModules().build();
JsonMapper mapper = JsonMapper.builder().addMixIn(Problem.class, ProblemJacksonMixIn.class).build();

Usage

Add library as dependency to Maven or Gradle. See the actual versions on Maven Central. Java 8 or higher is required to use problem4j-jackson library. Java 17 or higher is required to use problem4j-jackson3 library.

The problem4j-jackson modules does not declare jackson-databind as a transitive dependency. You should add jackson-databind explicitly as your main Jackson dependency.

For problem4j-jackson2 (Jackson 2.x):

  1. Maven:
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.20.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.problem4j</groupId>
            <artifactId>problem4j-core</artifactId>
            <version>1.3.0-RC1</version>
        </dependency>
        <dependency>
            <groupId>io.github.problem4j</groupId>
            <artifactId>problem4j-jackson2</artifactId>
            <version>1.3.0-RC1</version>
        </dependency>
    </dependencies>
  2. Gradle (Kotlin DSL):
    dependencies {
        implementation("com.fasterxml.jackson.core:jackson-databind:2.20.0")
        implementation("io.github.problem4j:problem4j-core:1.3.0-RC1")
        implementation("io.github.problem4j:problem4j-jackson2:1.3.0-RC1")
    }

For problem4j-jackson3 (Jackson 3.x):

  1. Maven:
    <dependencies>
        <dependency>
            <groupId>tools.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>io.github.problem4j</groupId>
            <artifactId>problem4j-core</artifactId>
            <version>1.3.0-RC1</version>
        </dependency>
        <dependency>
            <groupId>io.github.problem4j</groupId>
            <artifactId>problem4j-jackson3</artifactId>
            <version>1.3.0-RC1</version>
        </dependency>
    </dependencies>
  2. Gradle (Kotlin DSL):
    dependencies {
        implementation("tools.jackson.core:jackson-databind:3.0.3")
        implementation("io.github.problem4j:problem4j-core:1.3.0-RC1")
        implementation("io.github.problem4j:problem4j-jackson3:1.3.0-RC1")
    }

Problem4J Links

  • problem4j-core - Core library defining Problem model and ProblemException.
  • problem4j-jackson - Jackson module for serializing and deserializing Problem objects.
  • problem4j-spring - Spring modules extending ResponseEntityExceptionHandler for handling exceptions and returning Problem responses.

Building from source

Expand...

Gradle 9.x+ requires Java 17+ to run, but higher Java versions can also be used.

  • Module problem4j-jackson is compiled using a Java 8 toolchain, so the produced artifacts are compatible with Java 8.
  • Module problem4j-jackson3 is compiled using a Java 17 toolchain, so the produced artifacts are compatible with Java 17.
./gradlew build

To execute tests use test task.

./gradlew test

To format the code according to the style defined in build.gradle.kts rules use spotlessApply task. Note that building will fail if code is not properly formatted.

./gradlew spotlessApply

To publish the built artifacts to local Maven repository, run following command, replacing XXXX with the desired version. By default, the version is derived from git commit hash.

./gradlew -Pversion=XXXX publishToMavenLocal