Skip to content

Commit 3d9c18d

Browse files
committed
Update README.md
1 parent ef8af74 commit 3d9c18d

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,24 @@ import org.springframework.web.context.request.WebRequest;
4949
@RestControllerAdvice
5050
public class ExampleExceptionAdvice {
5151

52-
@ExceptionHandler(ExampleException.class)
53-
public ResponseEntity<Problem> method(ExampleException ex, WebRequest request) {
54-
Problem problem =
55-
Problem.builder()
56-
.type("http://example.com/errors/example-error")
57-
.title("Example Title")
58-
.status(400)
59-
.detail(ex.getMessage())
60-
.instance("https://example.com/instances/example-instance")
61-
.build();
62-
63-
HttpHeaders headers = new HttpHeaders();
64-
headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
65-
66-
HttpStatus status = HttpStatus.valueOf(problem.getStatus());
67-
68-
return new ResponseEntity<>(problem, headers, status);
69-
}
52+
@ExceptionHandler(ExampleException.class)
53+
public ResponseEntity<Problem> method(ExampleException ex, WebRequest request) {
54+
Problem problem =
55+
Problem.builder()
56+
.type("http://example.com/errors/example-error")
57+
.title("Example Title")
58+
.status(400)
59+
.detail(ex.getMessage())
60+
.instance("https://example.com/instances/example-instance")
61+
.build();
62+
63+
HttpHeaders headers = new HttpHeaders();
64+
headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
65+
66+
HttpStatus status = HttpStatus.valueOf(problem.getStatus());
67+
68+
return new ResponseEntity<>(problem, headers, status);
69+
}
7070
}
7171
```
7272

problem4j-spring-web/README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
# Built-in Spring Exception Mappings
1+
# Mapping exceptions to `application/problem+json` responses
2+
3+
1. [Overview](#overview)
4+
2. [Returning response bodies from custom exceptions](#returning-response-bodies-from-custom-exceptions)
5+
1. [Extending `ProblemException`](#extending-problemexception)
6+
2. [Annotating `@ProblemMapping`](#annotating-problemmapping)
7+
3. [Validation](#validation)
8+
4. [Occurrences of `TypeMismatchException`](#occurrences-of-typemismatchexception)
9+
5. [Occurrences of `ErrorResponseException`](#occurrences-of-errorresponseexception)
10+
6. [General HTTP Stuff](#general-http-stuff)
11+
12+
## Overview
213

314
This module overrides Spring Web's default (often minimal or plain-text) responses for many framework exceptions and
415
produces structured RFC 7807 `Problem` objects. [`ExceptionMappingConfiguration`][ExceptionMappingConfiguration]
@@ -67,7 +78,7 @@ To extract values from target exception, it's possible to use placeholders for i
6778

6879
```java
6980
/**
70-
* <pre>{@code
81+
* <pre>{@code
7182
* {
7283
* "type": "https://example.com/errors/invalid-request",
7384
* "title": "Invalid Request",
@@ -127,7 +138,9 @@ property `spring.validation.method.adapt-constraint-violations` to `true`. Enabl
127138
not rely on raw `ConstraintViolationException`, but rather on `MethodValidationException`, which contains more details
128139
about validated element.
129140

130-
Let's say we have following `@RestController`, where `idx` query param has different Java parameter name.
141+
Let's say we have following `@RestController`, where `customerId` query param has different Java parameter name (its
142+
`String customerIdParam`). We would like to have `customerId` in our response body as potential API clients do not have
143+
knowledge about internal technologies used by backend.
131144

132145
```java
133146
@Validated
@@ -175,6 +188,8 @@ or not. For `true` it will use value from `@RequestParam` (if able) (the same go
175188
</tr>
176189
</table>
177190

191+
*Note* that this is not build-in behaviour. It was implemented in [`MethodValidationMapping`][MethodValidationMapping].
192+
178193
## Occurrences of `TypeMismatchException`
179194

180195
Triggered for example when trying to pass `String` value into `@RequestParam("param") Integer param`.
@@ -240,7 +255,7 @@ Example:
240255
}
241256
```
242257
5. If passing request that's too large by configuration, service will write following response. Note that reason phrase
243-
for `413` was changed into `Content Too Large` in [RFC 9110 §15.5.14][rfc9110-15.5.4].
258+
for `413` was changed into `Content Too Large` in [RFC 9110 §15.5.14][rfc9110-15.5.4].
244259
```json
245260
{
246261
"status": 413,
@@ -253,3 +268,5 @@ Example:
253268
[ExceptionMapping]: src/main/java/io/github/malczuuu/problem4j/spring/web/mapping/ExceptionMapping.java
254269

255270
[ExceptionMappingConfiguration]: src/main/java/io/github/malczuuu/problem4j/spring/web/mapping/ExceptionMappingConfiguration.java
271+
272+
[MethodValidationMapping]: src/main/java/io/github/malczuuu/problem4j/spring/web/mapping/MethodValidationMapping.java

problem4j-spring-webflux/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ RFC 7807 `Problem` objects, with exceptions that are specific to `spring-webflux
1616
8. What happens if HTTP method (`GET`,`POST`, etc.) is invalid - [`MethodNotAllowedTest`][MethodNotAllowedTest].
1717
9. What happens if unknown endpoint (or resource) is accessed - [`NotFoundTest`][NotFoundTest].
1818
10. What happens if `ErrorResponseException` is thrown - [`ErrorResponseTest`][ErrorResponseTest].
19+
11. What happens if `ProblemException` is thrown [`ProblemFluxAdviceTest`][ProblemFluxAdviceTest] (or exception
20+
annotated with`@ProblemMapping`).
1921

2022
[MissingParameterTest]: src/test/java/io/github/malczuuu/problem4j/spring/webflux/integration/MissingParameterTest.java
2123

@@ -36,3 +38,5 @@ RFC 7807 `Problem` objects, with exceptions that are specific to `spring-webflux
3638
[NotFoundTest]: src/test/java/io/github/malczuuu/problem4j/spring/webflux/integration/NotFoundTest.java
3739

3840
[ErrorResponseTest]: src/test/java/io/github/malczuuu/problem4j/spring/webflux/integration/ErrorResponseTest.java
41+
42+
[ProblemFluxAdviceTest]: src/test/java/io/github/malczuuu/problem4j/spring/webflux/integration/ProblemFluxAdviceTest.java

problem4j-spring-webmvc/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ controller never leaks.
3131
8. What happens if HTTP method (`GET`,`POST`, etc.) is invalid - [`MethodNotAllowedTest`][MethodNotAllowedTest].
3232
9. What happens if unknown endpoint (or resource) is accessed - [`NotFoundTest`][NotFoundTest].
3333
10. What happens if `ErrorResponseException` is thrown - [`ErrorResponseTest`][ErrorResponseTest].
34+
11. What happens if `ProblemException` is thrown [`ProblemMvcAdviceTest`][ProblemMvcAdviceTest] (or exception
35+
annotated with`@ProblemMapping`).
3436

3537
[MissingParameterTest]: src/test/java/io/github/malczuuu/problem4j/spring/webmvc/integration/MissingParameterTest.java
3638

@@ -51,3 +53,5 @@ controller never leaks.
5153
[NotFoundTest]: src/test/java/io/github/malczuuu/problem4j/spring/webmvc/integration/NotFoundTest.java
5254

5355
[ErrorResponseTest]: src/test/java/io/github/malczuuu/problem4j/spring/webmvc/integration/ErrorResponseTest.java
56+
57+
[ProblemMvcAdviceTest]: src/test/java/io/github/malczuuu/problem4j/spring/webmvc/integration/ProblemMvcAdviceTest.java

0 commit comments

Comments
 (0)