Skip to content

Commit c32299f

Browse files
committed
Use HttpMessageNotWritableException instead of ISE
As a follow-up to the recent commit #37f9ce, this change replaces the raised IllegalStateException with HttpMessageNotWritableException. See gh-23205
1 parent 68c99da commit c32299f

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.core.codec.Hints;
3434
import org.springframework.http.MediaType;
3535
import org.springframework.http.codec.HttpMessageWriter;
36+
import org.springframework.http.converter.HttpMessageNotWritableException;
3637
import org.springframework.lang.Nullable;
3738
import org.springframework.util.Assert;
3839
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
@@ -163,7 +164,7 @@ protected Mono<Void> writeBody(@Nullable Object body, MethodParameter bodyParame
163164

164165
MediaType contentType = exchange.getResponse().getHeaders().getContentType();
165166
if (contentType != null && contentType.equals(bestMediaType)) {
166-
return Mono.error(new IllegalStateException(
167+
return Mono.error(new HttpMessageNotWritableException(
167168
"No Encoder for [" + elementType + "] with preset Content-Type '" + contentType + "'"));
168169
}
169170

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandlerTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.http.codec.ResourceHttpMessageWriter;
5252
import org.springframework.http.codec.json.Jackson2JsonEncoder;
5353
import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
54+
import org.springframework.http.converter.HttpMessageNotWritableException;
5455
import org.springframework.mock.web.test.server.MockServerWebExchange;
5556
import org.springframework.util.ObjectUtils;
5657
import org.springframework.web.reactive.HandlerResult;
@@ -365,7 +366,7 @@ public void handleWithPresetContentTypeShouldFailWithServerError() {
365366

366367
StepVerifier.create(resultHandler.handleResult(exchange, result))
367368
.consumeErrorWith(ex -> assertThat(ex)
368-
.isInstanceOf(IllegalStateException.class)
369+
.isInstanceOf(HttpMessageNotWritableException.class)
369370
.hasMessageContaining("with preset Content-Type"))
370371
.verify();
371372
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ protected <T> void writeWithMessageConverters(T value, MethodParameter returnTyp
176176
* @throws IOException thrown in case of I/O errors
177177
* @throws HttpMediaTypeNotAcceptableException thrown when the conditions indicated
178178
* by the {@code Accept} header on the request cannot be met by the message converters
179+
* @throws HttpMessageNotWritableException thrown if a given message cannot
180+
* be written by a converter, or if the content-type chosen by the server
181+
* has no compatible converter.
179182
*/
180183
@SuppressWarnings({"rawtypes", "unchecked"})
181184
protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
@@ -306,7 +309,7 @@ else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
306309

307310
if (body != null) {
308311
if (isContentTypePreset) {
309-
throw new IllegalStateException(
312+
throw new HttpMessageNotWritableException(
310313
"No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
311314
}
312315
throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.http.RequestEntity;
4848
import org.springframework.http.ResponseEntity;
4949
import org.springframework.http.converter.HttpMessageConverter;
50+
import org.springframework.http.converter.HttpMessageNotWritableException;
5051
import org.springframework.mock.web.test.MockHttpServletRequest;
5152
import org.springframework.mock.web.test.MockHttpServletResponse;
5253
import org.springframework.web.HttpMediaTypeNotAcceptableException;
@@ -59,7 +60,7 @@
5960
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
6061
import static org.assertj.core.api.Assertions.assertThat;
6162
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
62-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
63+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6364
import static org.mockito.ArgumentMatchers.any;
6465
import static org.mockito.ArgumentMatchers.anyCollection;
6566
import static org.mockito.ArgumentMatchers.argThat;
@@ -325,10 +326,11 @@ public void shouldFailWithServerErrorIfContentTypeFromResponseEntity() {
325326
given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
326327
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
327328

328-
assertThatIllegalStateException()
329-
.isThrownBy(() ->
330-
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest))
331-
.withMessageContaining("with preset Content-Type");
329+
assertThatThrownBy(() ->
330+
processor.handleReturnValue(
331+
returnValue, returnTypeResponseEntity, mavContainer, webRequest))
332+
.isInstanceOf(HttpMessageNotWritableException.class)
333+
.hasMessageContaining("with preset Content-Type");
332334
}
333335

334336
@Test

0 commit comments

Comments
 (0)