Interface ProblemMapper<T extends Throwable>

Type Parameters:
T - The Throwable subtype this mapper handles.

public interface ProblemMapper<T extends Throwable>
SPI for mapping a domain or framework Throwable into an RFC 7807 / 9457 Problem bean.

Implementations contribute domain-specific error translation without subclassing the request-processor chain. A ProblemMapper bean is registered via the resource bean store (e.g. a @Bean ProblemMapper foo() factory method on a @Rest-annotated resource, or any other mechanism that contributes named beans to the resource's bean store); the server-side processor picks it up automatically when an opted-in operation throws or returns a matching exception.

Discovery model

The processor collects all ProblemMapper beans visible from the resource's bean store, then picks the most-specific mapper for the exception class hierarchy (a mapper whose getExceptionType() is the closest superclass of the thrown exception wins; ties are broken by registration order, with the bean-store walk order serving as the tiebreaker). Mappers that return null are skipped and the next-most-specific mapper is tried, finally falling back to the default ProblemAdapters.fromException(...) adaptation in juneau-rest-common for BasicHttpException subclasses.

Example:

public class MyDomainProblemMapper implements ProblemMapper<MyDomainException> { @Override public Class<MyDomainException> getExceptionType() { return MyDomainException.class; } @Override public Problem map(MyDomainException e) { return Problem.fromStatus(422, "Domain rule violated", e.getMessage()) .setType(new URI("https://example.com/probs/domain")) .set("rule", e.getRuleId()); } } @Rest(problemDetails="true") public class MyResource { // Bean-store registration: factory method on the resource class. @Bean public ProblemMapper<MyDomainException> myMapper() { return new MyDomainProblemMapper(); } @RestGet("/buy") public Receipt buy() { throw new MyDomainException(...); // translated by myMapper() } }

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the exception type this mapper handles.
    map(T exception)
    Maps the supplied exception to a Problem bean.
  • Method Details

    • getExceptionType

      Returns the exception type this mapper handles.

      The processor matches mappers by checking getExceptionType().isAssignableFrom(thrown.getClass()) for each registered mapper, then picks the mapper whose getExceptionType() sits closest to the thrown class in the hierarchy. Implementations usually return the same class their <T> parameter binds; declaring it explicitly here avoids reflective inspection of the generic type at runtime.

      Returns:
      The exception class this mapper handles. Must not be null.
    • map

      Problem map(T exception)
      Maps the supplied exception to a Problem bean.

      Returning null signals "no opinion on this exception"; the processor then tries the next-most-specific registered mapper (and, ultimately, the built-in default fallback). Returning a populated Problem causes the processor to emit that bean as application/problem+json.

      Parameters:
      exception - The exception to translate. Never null.
      Returns:
      The Problem bean to emit, or null to defer to the next mapper.