Skip to content

malczuuu/problem4j-core

Repository files navigation

Problem4J Core

Build Status Sonatype License

Part of problem4j package of libraries.

This library provides a minimal, framework-agnostic Java model of the RFC 7807 "Problem Details" object, with an immutable Problem class and a fluent ProblemBuilder for convenient construction.

It is intended to be used as a foundation for other libraries or applications that add framework-specific behavior (e.g. Jackson, Spring).

Table of Contents

Features

  • ✅ Immutable Problem data model
  • ✅ Dedicated unchecked ProblemException to be used in error handling
  • ✅ Builder pattern for fluent construction
  • ✅ Serializable and easy to log or format
  • ✅ HTTP-agnostic (no external dependencies)
  • ✅ Follows RFC 7807 semantics:
    • type (URI)
    • title (short summary)
    • status (numeric code)
    • detail (detailed description)
    • instance (URI to the specific occurrence)
    • custom field extensions

Example

import io.github.malczuuu.problem4j.core.Problem;
import io.github.malczuuu.problem4j.core.ProblemException;

public class ExampleClass {

  public void method() {
    Problem problem =
        Problem.builder()
            .type("https://example.com/errors/invalid-request")
            .title("Invalid Request")
            .status(400)
            .detail("not a valid json")
            .instance("https://example.com/instances/1234")
            .build();

    throw new ProblemException(problem);
  }
}

Usage

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.

  1. Maven:
    <dependencies>
        <dependency>
            <groupId>io.github.malczuuu.problem4j</groupId>
            <artifactId>problem4j-core</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>
  2. Gradle (Groovy or Kotlin DSL):
    dependencies {
        implementation("io.github.malczuuu.problem4j:problem4j-core:1.1.0")
    }

Problem4J Links

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

About

Core library implementing Problem model according to RFC7807

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published