Problem4J & Javalin
The example integration of Problem4J with Javalin.
Even though Problem4J is primarily focused on Spring Boot, it can be easily integrated with any Java or Kotlin application,
as long as you can catch exceptions and convert them to Problem instances. This page demonstrates how to do this with
Javalin, a lightweight web framework using Kotlin.
Getting Started
<dependencies>
<dependency>
<groupId>io.github.problem4j</groupId>
<artifactId>problem4j-core</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>io.github.problem4j</groupId>
<artifactId>problem4j-jackson3</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
dependencies {
implementation("io.github.problem4j:problem4j-core:2.0.1")
implementation("io.github.problem4j:problem4j-jackson3:2.0.0")
}
Note that this example also assumes, for JSON serialization, that you use Jackson (JsonMapper) in either v2 or v3 and
the compatible Problem4J Jackson module.
val jsonMapper = JsonMapper.builder().findAndAddModules().build()
val problemMapper = DefaultProblemMapper()
val app = Javalin.create()
app.get("/hello") {
throw ProblemException(
Problem.of("Hello Error", 400, "something went wrong with /hello endpoint"))
}
app.exception(ProblemException::class.java) { ex, ctx ->
val problem = ex.problem
val json = jsonMapper.writeValueAsString(problem)
ctx.status(problem.status).result(json).contentType(Problem.CONTENT_TYPE)
}
app.exception(Exception::class.java) { ex, ctx ->
var problem = Problem.of(500)
if (problemMapper.isMappingCandidate(ex)) {
problem = problemMapper.toProblemBuilder(ex).build()
}
val json = jsonMapper.writeValueAsString(problem)
ctx.status(problem.status).result(json).contentType(Problem.CONTENT_TYPE)
}
app.start(7676)
The above example demonstrates support for both throwing ProblemException instances and @ProblemMapping-annotated
exceptions. The former is a simple wrapper around Problem objects, while the latter relies on the ProblemMapper to
convert exceptions into Problem instances.