Skip to content

Commit e679eb4

Browse files
committed
Update docs
1 parent 87d8e13 commit e679eb4

File tree

5 files changed

+132
-24
lines changed

5 files changed

+132
-24
lines changed

docs/01-problem4j-core/_category_.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
---
2-
sidebar_position: 1
2+
sidebar_position: 2
33
---
44

5-
# Setting Up & Configuration
6-
7-
Setting up dependencies for using Problem4J Core library.
8-
9-
## Dependency
5+
# Problem4J Core
106

117
Add library as dependency to Maven or Gradle. See the actual versions on [Maven Central][maven-central]. **Java 8** or
128
higher is required to use this library.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Problem4J & Spring Boot
6+
7+
The premier integration of Problem4J with Spring Boot.
8+
9+
## Getting Started
10+
11+
```xml
12+
<dependencies>
13+
<dependency>
14+
<groupId>io.github.problem4j</groupId>
15+
<artifactId>problem4j-spring-webmvc</artifactId>
16+
<version>2.2.0</version>
17+
</dependency>
18+
</dependencies>
19+
```
20+
21+
```kt
22+
dependencies {
23+
implementation("io.github.problem4j:problem4j-spring-webmvc:2.2.0")
24+
}
25+
```
26+
27+
Integration with Spring Boot is very straightforward. By adding the library as a dependency to your project, you get
28+
auto-configuration for Spring WebFlux and Spring WebMVC. This means that no additional configuration is required to
29+
start using Problem4J in your application. You can throw `ProblemException` or any exception annotated with
30+
`@ProblemMapping` from your controllers and they will be properly handled by the library.
31+
32+
Various Spring Boot internal exceptions are also mapped to `Problem` instances, so you can be sure that any exception
33+
thrown from your application will be handled and returned in a consistent format.
34+
35+
```java
36+
@RestController
37+
public class HelloController {
38+
39+
@GetMapping("/hello")
40+
public String hello() {
41+
throw new ProblemException(Problem.of("Hello Error", 400, "something went wrong with /hello endpoint"));
42+
}
43+
}
44+
```
45+
46+
The most prominent build-in exceptions that are mapped to `Problem` instances include:
47+
48+
- various validation errors,
49+
- missing request parameters,
50+
- type mismatches,
51+
- unsupported media types,
52+
- binding between request parameters and method arguments,
53+
- and many more.
54+
55+
Format for all these exceptions aims to encapsulate all internal details of the framework used by the application.
56+
57+
---
58+
59+
To peek, log or debug exceptions handled by the library, you can implement `AdviceWebMvcInspector` interface. This
60+
interface provide a single method that is called whenever an exception is handled by the library. You can use this
61+
method to log the exception or perform any other custom logic.
62+
63+
For WebFlux, use `AdviceWebFluxInspector` interface instead, which declares `ServerWebExchange` argument instead of
64+
`WebRequest`.
65+
66+
```java
67+
@Component
68+
public class LoggingInspector implements AdviceWebMvcInspector {
69+
70+
private static final Logger log = LoggerFactory.getLogger(LoggingInspector.class);
71+
72+
@Override
73+
public void inspect(
74+
ProblemContext context,
75+
Problem problem,
76+
Exception ex,
77+
HttpHeaders headers,
78+
HttpStatusCode status,
79+
WebRequest request) { // AdviceWebFluxInspector declares ServerWebExchange argument
80+
log.info(
81+
"Handled [status={} title={}]: exception={}",
82+
status.value(),
83+
problem.getTitle(),
84+
ex.getClass().getSimpleName());
85+
}
86+
}
87+
```
88+
89+
---
90+
91+
And with this, you have a basic setup of Problem4J with Spring Boot. You can now start throwing `ProblemException` from
92+
your controllers and they will be properly handled and returned in a consistent format.

docs/01-problem4j-core/02-using-it-from-scratch.md renamed to docs/05-tutorials/02-problem4j-and-javalin.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22
sidebar_position: 2
33
---
44

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
5+
# Problem4J & Javalin
6+
7+
The example integration of Problem4J with Javalin.
8+
9+
Even though Problem4J is primarily focused on Spring Boot, it can be easily integrated with any Java application, as
10+
long as you can catch exceptions and convert them to `Problem` instances. This page demonstrates how to do this with
11+
[Javalin][javalin], a lightweight web framework for Java and Kotlin.
12+
13+
## Getting Started
14+
15+
```xml
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.github.problem4j</groupId>
19+
<artifactId>problem4j-core</artifactId>
20+
<version>1.4.1</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>io.github.problem4j</groupId>
24+
<artifactId>problem4j-jackson3</artifactId>
25+
<version>1.4.1</version>
26+
</dependency>
27+
</dependencies>
28+
```
1329

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.
30+
```kt
31+
dependencies {
32+
implementation("io.github.problem4j:problem4j-core:1.4.1")
33+
implementation("io.github.problem4j:problem4j-jackson3:1.4.1")
34+
}
35+
```
1636

1737
Note that this example also assumes, for JSON serialization, that you use Jackson (`JsonMapper`) in either v2 or v3 and
1838
the compatible [Problem4J Jackson](../problem4j-jackson) module.

docs/05-tutorials/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Tutorials",
3+
"position": 5,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "Tutorials for learning about Problem4J - setting up and getting started."
7+
}
8+
}

0 commit comments

Comments
 (0)