Gson integration module for problem4j-core. Provides easy support for serializing and deserializing
the Problem model using Gson's GsonBuilder.
Note that RFC 7807 was later extended in RFC 9457, however core concepts remain the same.
- Seamless JSON serialization of
Problemobjects. - Compatible with standard Gson
GsonBuilder. - Pluggable via Gson's
TypeAdapterFactorymechanism. - Lightweight, with no external dependencies beyond Gson and
problem4j-core. - Supports Java Platform Module System. Artifact uses multi-release JAR to support Java 8 and Java 9+ module system.
Serialize and deserialize a Problem object using Gson.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.problem4j.core.Problem;
import io.github.problem4j.gson.ProblemTypeAdapterFactory;
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new ProblemTypeAdapterFactory())
.create();
Problem problem =
Problem.builder()
.title("Bad Request")
.status(400)
.detail("not a valid json")
.build();
String json = gson.toJson(problem, Problem.class);
Problem parsed = gson.fromJson(json, Problem.class);If using Java module system, add the following requires directive to your module-info.java file.
module org.example.project {
requires io.github.problem4j.gson;
}Add library as dependency to Maven or Gradle. See the actual versions on Maven Central. Java 8 or higher is required to use this library.
Important
The problem4j-gson module does not declare gson as a transitive dependency. You must include problem4j-core
as a dependency in your project.
- Maven:
<dependencies> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.14.0</version> </dependency> <dependency> <groupId>io.github.problem4j</groupId> <artifactId>problem4j-core</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>io.github.problem4j</groupId> <artifactId>problem4j-gson</artifactId> <version>{version}</version> </dependency> </dependencies>
- Gradle (Groovy or Kotlin DSL):
dependencies { implementation("com.google.code.gson:gson:2.14.0") implementation("io.github.problem4j:problem4j-core:2.0.0") implementation("io.github.problem4j:problem4j-gson:{version}") }
Problem4J Gson is considered feature complete. Only bug fixes will be added. New features may be included only if there is a strong justification for them; otherwise, future projects are expected to build on this one as a dependency.
problem4j.github.io- Full documentation of all projects from Problem4J family.problem4j-core- Core library definingProblemmodel andProblemException.problem4j-gson- Gson module for serializing and deserializingProblemobjects.problem4j-jackson- Jackson module for serializing and deserializingProblemobjects.problem4j-spring- Spring modules extendingResponseEntityExceptionHandlerfor handling exceptions and returningProblemresponses.
Expand...
Gradle 9.x+ requires Java 17 or higher to run. For building the project, Gradle automatically picks up Java
25 via toolchains and the foojay-resolver-convention plugin. This Java version is needed because the project
uses ErrorProne and NullAway for static nullness analysis.
The produced artifacts are compatible with Java 8 thanks to options.release = 8 in the Gradle JavaCompile task.
This means that regardless of the Java version used to run Gradle, the resulting bytecode remains compatible.
The default Gradle tasks include spotlessApply (for code formatting) and build (for compilation and tests). The
simplest way to build the project is to run:
./gradlewTo execute tests use test task. Tests do not change options.release so newer Java API can be used.
./gradlew testTo 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 spotlessApplyNote that if the year has changed, add -Pspotless.license-year-enabled flag to update the year in license headers.
The publishing GitHub Action will fail if the year is not updated, but
for local development and builds you can choose to skip it and update the year later.
./gradlew spotlessApply -Pspotless.license-year-enabledTo publish the built artifacts to local Maven repository, use publishToMavenLocal task.
./gradlew publishToMavenLocalNote that for using Maven Local artifacts in target projects, you need to add mavenLocal() repository.
repositories {
mavenLocal()
mavenCentral()
}Note that the following warning during build is expected and can be ignored. It appears because the module-info is compiled against the main sources without Gson on the module path:
> Task :compileMain9Java
./src/main9/java/module-info.java: warning: [module] module not found: com.google.gson