Skip to content

Commit 45ee791

Browse files
committed
Polishing ProblemDetail support
See gh-28665
1 parent 16c43c2 commit 45ee791

File tree

7 files changed

+18
-14
lines changed

7 files changed

+18
-14
lines changed

spring-web/src/main/java/org/springframework/http/ProblemDetail.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323

2424
/**
2525
* Representation of an RFC 7807 problem detail, including all RFC-defined
26-
* fields. For an extended response with more fields, create a subclass that
27-
* exposes the additional fields.
26+
* properties.
27+
*
28+
* <p>For an extended response, create a subclass with additional properties.
29+
* A subclass can use the {@link ProblemDetail#ProblemDetail(ProblemDetail)}
30+
* copy constructor to extend an existing {@code ProblemDetail} instance.
2831
*
2932
* @author Rossen Stoyanchev
3033
* @since 6.0
@@ -63,8 +66,8 @@ protected ProblemDetail(int rawStatusCode) {
6366
}
6467

6568
/**
66-
* Copy constructor that could be used from a subclass to re-create a
67-
* {@code ProblemDetail} in order to extend it with more fields.
69+
* Copy constructor that a subclass can use to re-create and extend a
70+
* {@code ProblemDetail} with additional properties.
6871
*/
6972
protected ProblemDetail(ProblemDetail other) {
7073
this.type = other.type;
@@ -75,7 +78,7 @@ protected ProblemDetail(ProblemDetail other) {
7578
}
7679

7780
/**
78-
* For deserialization.
81+
* No-arg constructor, for deserialization.
7982
*/
8083
protected ProblemDetail() {
8184
}

spring-web/src/main/java/org/springframework/http/ResponseEntity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,11 @@ public static <T> ResponseEntity<T> of(Optional<T> body) {
264264

265265
/**
266266
* Create a builder for a {@code ResponseEntity} with the given
267-
* {@link ProblemDetail} as the body, also matching to its
268-
* {@link ProblemDetail#getStatus() status}. An {@code @ExceptionHandler}
269-
* method can use to add response headers, or otherwise it can return
270-
* {@code ProblemDetail}.
267+
* {@link ProblemDetail} as the body, and its
268+
* {@link ProblemDetail#getStatus() status} as the status.
269+
* <p>Note that {@code ProblemDetail} is supported as a return value from
270+
* controller methods and from {@code @ExceptionHandler} methods. The method
271+
* here is convenient to also add response headers.
271272
* @param body the details for an HTTP error response
272273
* @return the created builder
273274
* @since 6.0

spring-web/src/main/java/org/springframework/web/ErrorResponseException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* <p>The exception can be used as is, or it can be extended as a more specific
3333
* exception that populates the {@link ProblemDetail#setType(URI) type} or
3434
* {@link ProblemDetail#setDetail(String) detail} fields, or potentially adds
35-
* other non-standard fields.
35+
* other non-standard properties.
3636
*
3737
* @author Rossen Stoyanchev
3838
* @since 6.0

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
167167
throw ex;
168168
}
169169

170-
// Fall back on RFC 7807 format for ProblemDetail
170+
// For ProblemDetail, fall back on RFC 7807 format
171171
if (bestMediaType == null && elementType.toClass().equals(ProblemDetail.class)) {
172172
bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType), this.problemMediaTypes);
173173
}

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ else if (returnValue instanceof ErrorResponse response) {
144144
httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getStatusCode());
145145
}
146146
else if (returnValue instanceof ProblemDetail detail) {
147-
httpEntity = new ResponseEntity<>(returnValue, HttpHeaders.EMPTY, detail.getStatus());
147+
httpEntity = ResponseEntity.of(detail).build();
148148
}
149149
else if (returnValue instanceof HttpHeaders) {
150150
httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK);

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter
243243
List<MediaType> compatibleMediaTypes = new ArrayList<>();
244244
determineCompatibleMediaTypes(acceptableTypes, producibleTypes, compatibleMediaTypes);
245245

246-
// Fall back on RFC 7807 format for ProblemDetail
246+
// For ProblemDetail, fall back on RFC 7807 format
247247
if (compatibleMediaTypes.isEmpty() && ProblemDetail.class.isAssignableFrom(valueType)) {
248248
determineCompatibleMediaTypes(this.problemMediaTypes, producibleTypes, compatibleMediaTypes);
249249
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
185185
httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getStatusCode());
186186
}
187187
else if (returnValue instanceof ProblemDetail detail) {
188-
httpEntity = new ResponseEntity<>(returnValue, HttpHeaders.EMPTY, detail.getStatus());
188+
httpEntity = ResponseEntity.of(detail).build();
189189
}
190190
else {
191191
Assert.isInstanceOf(HttpEntity.class, returnValue);

0 commit comments

Comments
 (0)