Skip to content

Commit 89e0273

Browse files
committed
Fix UnprocessableContent support in WebClientResponseException
Prior to this commit, `WebClientResponseException` would only support the deprecated "unprocessable entity" status. This commit adds the missing support for "unprocessable content" when creating exceptions with `WebClientResponseException#create`. Fixes gh-35802
1 parent 8c4b30a commit 89e0273

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ public static WebClientResponseException create(
314314
case GONE -> new WebClientResponseException.Gone(statusText, headers, body, charset, request);
315315
case UNSUPPORTED_MEDIA_TYPE -> new WebClientResponseException.UnsupportedMediaType(statusText, headers, body, charset, request);
316316
case TOO_MANY_REQUESTS -> new WebClientResponseException.TooManyRequests(statusText, headers, body, charset, request);
317+
case UNPROCESSABLE_CONTENT -> new WebClientResponseException.UnprocessableContent(statusText, headers, body, charset, request);
317318
case UNPROCESSABLE_ENTITY -> new WebClientResponseException.UnprocessableEntity(statusText, headers, body, charset, request);
318319
case INTERNAL_SERVER_ERROR -> new WebClientResponseException.InternalServerError(statusText, headers, body, charset, request);
319320
case NOT_IMPLEMENTED -> new WebClientResponseException.NotImplemented(statusText, headers, body, charset, request);
@@ -340,7 +341,7 @@ public static class BadRequest extends WebClientResponseException {
340341
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
341342
@Nullable HttpRequest request) {
342343

343-
super(HttpStatus.BAD_REQUEST.value(), statusText, headers, body, charset, request);
344+
super(HttpStatus.BAD_REQUEST, statusText, headers, body, charset, request);
344345
}
345346

346347
}
@@ -356,7 +357,7 @@ public static class Unauthorized extends WebClientResponseException {
356357
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
357358
@Nullable HttpRequest request) {
358359

359-
super(HttpStatus.UNAUTHORIZED.value(), statusText, headers, body, charset, request);
360+
super(HttpStatus.UNAUTHORIZED, statusText, headers, body, charset, request);
360361
}
361362
}
362363

@@ -371,7 +372,7 @@ public static class Forbidden extends WebClientResponseException {
371372
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
372373
@Nullable HttpRequest request) {
373374

374-
super(HttpStatus.FORBIDDEN.value(), statusText, headers, body, charset, request);
375+
super(HttpStatus.FORBIDDEN, statusText, headers, body, charset, request);
375376
}
376377
}
377378

@@ -386,7 +387,7 @@ public static class NotFound extends WebClientResponseException {
386387
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
387388
@Nullable HttpRequest request) {
388389

389-
super(HttpStatus.NOT_FOUND.value(), statusText, headers, body, charset, request);
390+
super(HttpStatus.NOT_FOUND, statusText, headers, body, charset, request);
390391
}
391392
}
392393

@@ -401,7 +402,7 @@ public static class MethodNotAllowed extends WebClientResponseException {
401402
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
402403
@Nullable HttpRequest request) {
403404

404-
super(HttpStatus.METHOD_NOT_ALLOWED.value(), statusText, headers, body, charset, request);
405+
super(HttpStatus.METHOD_NOT_ALLOWED, statusText, headers, body, charset, request);
405406
}
406407
}
407408

@@ -416,7 +417,7 @@ public static class NotAcceptable extends WebClientResponseException {
416417
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
417418
@Nullable HttpRequest request) {
418419

419-
super(HttpStatus.NOT_ACCEPTABLE.value(), statusText, headers, body, charset, request);
420+
super(HttpStatus.NOT_ACCEPTABLE, statusText, headers, body, charset, request);
420421
}
421422
}
422423

@@ -431,7 +432,7 @@ public static class Conflict extends WebClientResponseException {
431432
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
432433
@Nullable HttpRequest request) {
433434

434-
super(HttpStatus.CONFLICT.value(), statusText, headers, body, charset, request);
435+
super(HttpStatus.CONFLICT, statusText, headers, body, charset, request);
435436
}
436437
}
437438

@@ -446,7 +447,7 @@ public static class Gone extends WebClientResponseException {
446447
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
447448
@Nullable HttpRequest request) {
448449

449-
super(HttpStatus.GONE.value(), statusText, headers, body, charset, request);
450+
super(HttpStatus.GONE, statusText, headers, body, charset, request);
450451
}
451452
}
452453

@@ -461,7 +462,7 @@ public static class UnsupportedMediaType extends WebClientResponseException {
461462
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
462463
@Nullable HttpRequest request) {
463464

464-
super(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(), statusText, headers, body, charset, request);
465+
super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, statusText, headers, body, charset, request);
465466
}
466467
}
467468

@@ -476,7 +477,7 @@ public static class UnprocessableContent extends WebClientResponseException {
476477
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
477478
@Nullable HttpRequest request) {
478479

479-
super(HttpStatus.UNPROCESSABLE_CONTENT.value(), statusText, headers, body, charset, request);
480+
super(HttpStatus.UNPROCESSABLE_CONTENT, statusText, headers, body, charset, request);
480481
}
481482
}
482483

@@ -494,7 +495,7 @@ public static class UnprocessableEntity extends WebClientResponseException {
494495
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
495496
@Nullable HttpRequest request) {
496497

497-
super(HttpStatus.UNPROCESSABLE_ENTITY.value(), statusText, headers, body, charset, request);
498+
super(HttpStatus.UNPROCESSABLE_ENTITY, statusText, headers, body, charset, request);
498499
}
499500
}
500501

@@ -509,7 +510,7 @@ public static class TooManyRequests extends WebClientResponseException {
509510
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
510511
@Nullable HttpRequest request) {
511512

512-
super(HttpStatus.TOO_MANY_REQUESTS.value(), statusText, headers, body, charset, request);
513+
super(HttpStatus.TOO_MANY_REQUESTS, statusText, headers, body, charset, request);
513514
}
514515
}
515516

@@ -528,7 +529,7 @@ public static class InternalServerError extends WebClientResponseException {
528529
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
529530
@Nullable HttpRequest request) {
530531

531-
super(HttpStatus.INTERNAL_SERVER_ERROR.value(), statusText, headers, body, charset, request);
532+
super(HttpStatus.INTERNAL_SERVER_ERROR, statusText, headers, body, charset, request);
532533
}
533534
}
534535

@@ -543,7 +544,7 @@ public static class NotImplemented extends WebClientResponseException {
543544
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
544545
@Nullable HttpRequest request) {
545546

546-
super(HttpStatus.NOT_IMPLEMENTED.value(), statusText, headers, body, charset, request);
547+
super(HttpStatus.NOT_IMPLEMENTED, statusText, headers, body, charset, request);
547548
}
548549
}
549550

@@ -558,7 +559,7 @@ public static class BadGateway extends WebClientResponseException {
558559
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
559560
@Nullable HttpRequest request) {
560561

561-
super(HttpStatus.BAD_GATEWAY.value(), statusText, headers, body, charset, request);
562+
super(HttpStatus.BAD_GATEWAY, statusText, headers, body, charset, request);
562563
}
563564
}
564565

@@ -573,7 +574,7 @@ public static class ServiceUnavailable extends WebClientResponseException {
573574
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
574575
@Nullable HttpRequest request) {
575576

576-
super(HttpStatus.SERVICE_UNAVAILABLE.value(), statusText, headers, body, charset, request);
577+
super(HttpStatus.SERVICE_UNAVAILABLE, statusText, headers, body, charset, request);
577578
}
578579
}
579580

@@ -588,7 +589,7 @@ public static class GatewayTimeout extends WebClientResponseException {
588589
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset,
589590
@Nullable HttpRequest request) {
590591

591-
super(HttpStatus.GATEWAY_TIMEOUT.value(), statusText, headers, body, charset, request);
592+
super(HttpStatus.GATEWAY_TIMEOUT, statusText, headers, body, charset, request);
592593
}
593594
}
594595

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientResponseExceptionTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@
1616

1717
package org.springframework.web.reactive.function.client;
1818

19+
import java.util.stream.Stream;
20+
1921
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.http.HttpStatus;
2027

2128
import static org.assertj.core.api.Assertions.assertThat;
2229

2330
/**
2431
* Tests for {@link WebClientResponseException}.
2532
*
2633
* @author Simon Baslé
34+
* @author Brian Clozel
2735
*/
2836
class WebClientResponseExceptionTests {
2937

@@ -90,4 +98,17 @@ void constructWith5xxStatusCodeAndCauseNoAdditionalMessage() {
9098
assertThat(ex).hasMessage("500 reasonPhrase").hasRootCauseMessage("example cause");
9199
}
92100

101+
@ParameterizedTest
102+
@MethodSource("httpStatusValues")
103+
void createExceptionWithStatus(HttpStatus status) {
104+
WebClientResponseException exception = WebClientResponseException
105+
.create(status, "reasonPhrase", new HttpHeaders(), new byte[0], null, null);
106+
107+
assertThat(exception.getStatusCode()).isEqualTo(status);
108+
}
109+
110+
static Stream<HttpStatus> httpStatusValues() {
111+
return Stream.of(HttpStatus.values());
112+
}
113+
93114
}

0 commit comments

Comments
 (0)