Skip to content

Commit 87d8e13

Browse files
Add docs for using problem4j-core in a non-framework setup
1 parent da6251c commit 87d8e13

4 files changed

Lines changed: 84 additions & 16 deletions

File tree

docs/02-problem4j-core.md renamed to docs/01-problem4j-core/01-setting-up-and-configuration.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 1
33
---
44

5-
# Problem4J Core
5+
# Setting Up & Configuration
66

7-
Problem4J Core provides a framework-agnostic set of features to be used in implementing framework-specific features by
8-
other modules. There are two primary ways of throwing exception with `Problem`.
9-
10-
1. Throw a `ProblemException`.
11-
2. Throw exception annotated with `@ProblemMapping`.
12-
13-
Following chapters touch also other aspects, such as `ProblemContext` and how to approach altering `Problem` objects,
14-
if they are immutable.
7+
Setting up dependencies for using Problem4J Core library.
158

169
## Dependency
1710

@@ -35,7 +28,18 @@ higher is required to use this library.
3528
}
3629
```
3730

38-
## Throw a `ProblemException`
31+
## Usage
32+
33+
Problem4J Core provides a framework-agnostic set of features to be used in implementing framework-specific features by
34+
other modules. There are two primary ways of throwing exception with `Problem`.
35+
36+
1. Throw a `ProblemException`.
37+
2. Throw exception annotated with `@ProblemMapping`.
38+
39+
Following chapters touch also other aspects, such as `ProblemContext` and how to approach altering `Problem` objects,
40+
if they are immutable.
41+
42+
### Throw a `ProblemException`
3943

4044
The most basic method is to throw a `ProblemException` or its subclass.
4145

@@ -83,7 +87,7 @@ static Problem Problem.of(URI type, String title, int status);
8387
static Problem Problem.of(URI type, String title, int status, @Nullable String detail);
8488
```
8589

86-
## Throw exception annotated with `@ProblemMapping`
90+
### Throw exception annotated with `@ProblemMapping`
8791

8892
If an exception is annotated with `@ProblemMapping`, extracting the underlying `Problem` from it requires a bit of
8993
setup first. Fields such as `type`, `title`, `detail` and `instance` allow to interpolate fields from exception object
@@ -137,7 +141,7 @@ try {
137141
}
138142
```
139143

140-
## Including `ProblemContext` in `@ProblemMapping`-annotated exception
144+
### Including `ProblemContext` in `@ProblemMapping`-annotated exception
141145

142146
`ProblemMapper` allows passing `ProblemContext` instance as an additional argument to `toProblemBuilder` method. Such
143147
context allows to pass additional data for `@ProblemMapping` fields interpolation.
@@ -184,7 +188,7 @@ try {
184188
}
185189
```
186190

187-
## Modifying `Problem` objects
191+
### Modifying `Problem` objects
188192

189193
`Problem` objects are immutable. In order to change any of its values, you are required to create a new `Problem`
190194
object. For that purpose, consider using `toBuilder()` method.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Using it from scratch
6+
7+
An example of setting up with an actual HTTP server.
8+
9+
The primary integration provided by this library orbits around Spring Boot. However, the Problem4J libraries are fully
10+
featured to work in any environment.
11+
12+
## Integration example for Javalin
13+
14+
This chapter is a quick introduction to using Problem4J in a non-Spring Java application, using [Javalin][javalin]
15+
library for HTTP server functionality.
16+
17+
Note that this example also assumes, for JSON serialization, that you use Jackson (`JsonMapper`) in either v2 or v3 and
18+
the compatible [Problem4J Jackson](../problem4j-jackson) module.
19+
20+
```java
21+
JsonMapper jsonMapper = new JsonMapper.Builder().findAndAddModules().build();
22+
ProblemMapper problemMapper = ProblemMapper.create();
23+
24+
Javalin app = Javalin.create();
25+
26+
app.get("/hello", ctx -> {
27+
throw new ProblemException(
28+
Problem.of("Hello Error", 400, "something went wrong with /hello endpoint"));
29+
});
30+
31+
app.exception(ProblemException.class, (ex, ctx) -> {
32+
Problem problem = ex.getProblem();
33+
34+
String json = jsonMapper.writeValueAsString(problem);
35+
ctx.status(problem.getStatus()).result(json).contentType(Problem.CONTENT_TYPE);
36+
});
37+
38+
app.exception(Exception.class, (ex, ctx) -> {
39+
Problem problem = Problem.of(500);
40+
41+
if (problemMapper.isMappingCandidate(ex)) {
42+
problem = problemMapper.toProblemBuilder(ex).build();
43+
}
44+
45+
String json = jsonMapper.writeValueAsString(problem);
46+
ctx.status(problem.getStatus()).result(json).contentType(Problem.CONTENT_TYPE);
47+
});
48+
49+
app.start(7676);
50+
```
51+
52+
The above example demonstrates support for both throwing `ProblemException` instances and `@ProblemMapping`-annotated
53+
exceptions. The former is a simple wrapper around `Problem` objects, while the latter relies on the `ProblemMapper` to
54+
convert exceptions into `Problem` instances.
55+
56+
[javalin]: https://javalin.io/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Problem4J Core",
3+
"position": 2,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Learn about Problem4J Core - the base, framework-agnostic features of Problem4J."
7+
}
8+
}

docusaurus.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const config: Config = {
8888
// label: 'Docs',
8989
// },
9090
{to: '/docs/intro', label: 'Intro', position: 'left'},
91-
{to: '/docs/problem4j-core', label: 'Problem4J Core', position: 'left'},
91+
{to: '/docs/category/problem4j-core', label: 'Problem4J Core', position: 'left'},
9292
{to: '/docs/problem4j-jackson', label: 'Problem4J Jackson', position: 'left'},
9393
{to: '/docs/category/problem4j-spring', label: 'Problem4J Spring', position: 'left'},
9494
{to: '/blog', label: 'Blog', position: 'left'},
@@ -111,7 +111,7 @@ const config: Config = {
111111
},
112112
{
113113
label: 'Problem4J Core',
114-
to: '/docs/problem4j-core',
114+
to: '/docs/category/problem4j-core',
115115
},
116116
{
117117
label: 'Problem4J Jackson',

0 commit comments

Comments
 (0)