You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/asciidoc/problem-details.adoc
+18-11Lines changed: 18 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,18 +46,25 @@ problem.details {
46
46
47
47
==== Creating problems
48
48
49
-
`HttpProblem` class represents the `RFC 7807` model. It is the main entity you need to work with to produce the problem.
49
+
javadoc:problem.HttpProblem[] class represents the `RFC 7807` model. It is the main entity you need to work with to produce the problem.
50
50
51
51
===== Static helpers
52
52
53
-
There are several handy static methods to produce a simple `HttpProblem`:
53
+
There are several handy static methods to produce a simple javadoc:problem.HttpProblem[]:
54
54
55
-
- `HttpProblem.valueOf(StatusCode status)` - will pick the title by status code.
55
+
- javadoc:problem.HttpProblem[valueOf, io.jooby.StatusCode] - will pick the title by status code.
56
56
Don't overuse it, the problem should have meaningful `title` and `detail` when possible.
57
-
- `HttpProblem.valueOf(StatusCode status, String title)` - with custom `title`
58
-
- `HttpProblem.valueOf(StatusCode status, String title, String detail)` - with `title` and `detail`
57
+
- javadoc:problem.HttpProblem[valueOf, io.jooby.StatusCode, java.lang.String] - with custom `title`
58
+
- javadoc:problem.HttpProblem[valueOf, io.jooby.StatusCode, java.lang.String, java.lang.String] - with `title` and `detail`
59
59
60
-
`HttpProblem` extends `RuntimeException` so you can naturally throw it (as you do with exceptions):
60
+
and a couple of shorthands for the most common validation codes:
61
+
62
+
- javadoc:problem.HttpProblem[badRequest, java.lang.String, java.lang.String] - for _400 Bad Request_
63
+
- javadoc:problem.HttpProblem[notFound, java.lang.String, java.lang.String] - for _404 Not Found_
64
+
- javadoc:problem.HttpProblem[unprocessableEntity, java.lang.String, java.lang.String] - for _422 Unprocessable Entity_
65
+
- javadoc:problem.HttpProblem[internalServerError] - for _500 Internal Server Error_
66
+
67
+
javadoc:problem.HttpProblem[] extends `RuntimeException` so you can naturally throw it (as you do with exceptions):
61
68
62
69
.Java
63
70
[source,java,role="primary"]
@@ -130,7 +137,7 @@ throw HttpProblem.builder()
130
137
131
138
`RFC 7807` has a simple extension model: APIs are free to add any other properties to the problem details object, so all properties other than the five ones listed above are extensions.
132
139
133
-
However, variadic root level fields are usually not very convenient for (de)serialization (especially in statically typed languages). That's why `HttpProblem` implementation grabs all extensions under a single root field `parameters`. You can add parameters using builder like this:
140
+
However, variadic root level fields are usually not very convenient for (de)serialization (especially in statically typed languages). That's why javadoc:problem.HttpProblem[] implementation grabs all extensions under a single root field `parameters`. You can add parameters using builder like this:
134
141
135
142
[source,java]
136
143
----
@@ -165,7 +172,7 @@ Resulting response:
165
172
166
173
==== Adding headers
167
174
168
-
Some `HTTP` codes (like `413` or `426`) require additional response headers, or it may be required by third-party system/integration. `HttpProblem` support additional headers in response:
175
+
Some `HTTP` codes (like `413` or `426`) require additional response headers, or it may be required by third-party system/integration. javadoc:problem.HttpProblem[] support additional headers in response:
169
176
170
177
[source,java]
171
178
----
@@ -211,12 +218,12 @@ In response:
211
218
212
219
[TIP]
213
220
====
214
-
If you need to enrich errors with more information feel free to extend `HttpProblem.Error` and make your custom errors model.
221
+
If you need to enrich errors with more information feel free to extend javadoc:problem.HttpProblem.Error[] and make your custom errors model.
215
222
====
216
223
217
224
==== Custom `Exception` to `HttpProblem`
218
225
219
-
Apparently, you may already have many custom `Exception` classes in the codebase, and you want to make them `Problem Details` compliant without complete re-write. You can achieve this by implementing `HttpProblemMappable` interface. It allows you to control how exceptions should be transformed into `HttpProblem` if default behaviour doesn't suite your needs:
226
+
Apparently, you may already have many custom `Exception` classes in the codebase, and you want to make them `Problem Details` compliant without complete re-write. You can achieve this by implementing javadoc:problem.HttpProblemMappable[] interface. It allows you to control how exceptions should be transformed into javadoc:problem.HttpProblem if default behaviour doesn't suite your needs:
220
227
221
228
[source,java]
222
229
----
@@ -235,7 +242,7 @@ public class MyException implements HttpProblemMappable {
235
242
236
243
==== Custom Problems
237
244
238
-
Extending `HttpProblem` and utilizing builder functionality makes it really easy:
245
+
Extending javadoc:problem.HttpProblem[] and utilizing builder functionality makes it really easy:
0 commit comments