Designing clear and consistent error responses in a REST API is often harder than it looks. Without a shared standard, each application ends up inventing its own ad-hoc format, which quickly leads to inconsistency and confusion. RFC 7807 - Problem Details for HTTP APIs solves this by defining a simple, extensible JSON structure for error messages.
Problem4J brings this specification into the Spring ecosystem, offering a practical way to model, throw, and handle
API errors using Problem
objects. It helps you enforce a consistent error contract across your services, while staying
flexible enough for custom exceptions and business-specific details.
Even though Spring provides ProblemDetail
and ErrorResponseException
for RFC 7807-compliant error responses,
they are mutable, minimal, and often require manual population of fields. In contrast, Problem4J was created to:
- Provide a fully immutable, fluent
Problem
model with support for extensions. - Support declarative exception mapping via
@ProblemMapping
or programmatic one viaProblemException
. - Automatically interpolate exception fields and context metadata (e.g.,
traceId
) into responses. - Offer consistent error responses across WebMVC and WebFlux, including validation and framework exceptions.
- Allow custom extensions without boilerplate, making structured errors easier to trace and consume.
In short, Problem4J is designed for developers who want robust, traceable, and fully configurable REST API errors, while keeping everything RFC 7807-compliant.
This module provides Spring integration for problem4j-core
. library that integrates the RFC Problem
Details model with exception handling in Spring Boot.
- ✅ Automatic mapping of exceptions to responses with
Problem
objects compliant with RFC 7807. - ✅ Mapping of exceptions extending
ProblemException
to responses withProblem
objects. - ✅ Mapping of exceptions annotated with
@ProblemMapping
to responses withProblem
objects. - ✅ Fallback mapping of
Exception
toProblem
objects representing500 Internal Server Error
. - ✅ Simple configuration thanks to Spring Boot autoconfiguration.
The library provides two ways to convert exceptions into RFC 7807-compliant Problem
responses. You can either extend
ProblemException
or use @ProblemMapping
annotation on your own exception if modifying inheritance tree is not an
option for.
For more details and usage examples, see the submodule README.md
files:
problem4j-spring-web/README.md
,problem4j-spring-webflux/README.md
,problem4j-spring-webmvc/README.md
.
Add library as dependency to Maven or Gradle. See the actual versions on Maven Central. Add it along with repository in your dependency manager. Java 17 or higher is required to use this library.
Tested with Spring Boot 3+
, but mostly on 3.5.x
. However, the idea for v1.x
of this library was to be backwards
compatible down to Spring Boot 3.0.0
. Integration with Spring Boot 4 (once its released) will most likely be
released as v2.x
if v1.x
won't be compatible.
- Maven:
<dependencies> <!-- pick the one for your project --> <dependency> <groupId>io.github.malczuuu.problem4j</groupId> <artifactId>problem4j-spring-webflux</artifactId> <version>1.0.0-alpha2</version> </dependency> <dependency> <groupId>io.github.malczuuu.problem4j</groupId> <artifactId>problem4j-spring-webmvc</artifactId> <version>1.0.0-alpha2</version> </dependency> </dependencies>
- Gradle (Groovy or Kotlin DSL):
dependencies { // pick the one for your project implementation("io.github.malczuuu.problem4j:problem4j-spring-webflux:1.0.0-alpha2") implementation("io.github.malczuuu.problem4j:problem4j-spring-webmvc:1.0.0-alpha2") }
Library can be configured with following properties.
Property that specifies how exception handling imported with this module should print "detail"
field of Problem
model (lowercase
, capitalized
- default, uppercase
). Useful for keeping the same style of errors coming from
library and your application.
Property that specifies the name of the HTTP header used for tracing requests. If set, the trace identifier from this
header can be injected into the Problem
response, for example into theinstance
field when combined with
problem4j.instance-override
. Defaults to null
(disabled).
Property that defines a template for overriding the instance
field in Problem
responses.The value may contain the
special placeholder {traceId}
, which will be replaced at runtime with the trace identifier from the current request (
see problem4j.tracing-header-name
). Defaults to null
(no override applied).
problem4j-core
- Core library definingProblem
model andProblemException
.problem4j-jackson
- Jackson module for serializing and deserializingProblem
objects.problem4j-spring
- Spring modules extendingResponseEntityExceptionHandler
for handling exceptions and returningProblem
responses.