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
This commit is contained in:
Rossen Stoyanchev 2019-07-13 10:56:12 +01:00
parent 68c99dafcf
commit c32299f734
4 changed files with 15 additions and 8 deletions

View File

@ -33,6 +33,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Hints; import org.springframework.core.codec.Hints;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageWriter; import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver; import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
@ -163,7 +164,7 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa
MediaType contentType = exchange.getResponse().getHeaders().getContentType(); MediaType contentType = exchange.getResponse().getHeaders().getContentType();
if (contentType != null && contentType.equals(bestMediaType)) { if (contentType != null && contentType.equals(bestMediaType)) {
return Mono.error(new IllegalStateException( return Mono.error(new HttpMessageNotWritableException(
"No Encoder for [" + elementType + "] with preset Content-Type '" + contentType + "'")); "No Encoder for [" + elementType + "] with preset Content-Type '" + contentType + "'"));
} }

View File

@ -51,6 +51,7 @@ import org.springframework.http.codec.HttpMessageWriter;
import org.springframework.http.codec.ResourceHttpMessageWriter; import org.springframework.http.codec.ResourceHttpMessageWriter;
import org.springframework.http.codec.json.Jackson2JsonEncoder; import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.codec.xml.Jaxb2XmlEncoder; import org.springframework.http.codec.xml.Jaxb2XmlEncoder;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.mock.web.test.server.MockServerWebExchange; import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.web.reactive.HandlerResult; import org.springframework.web.reactive.HandlerResult;
@ -365,7 +366,7 @@ public class ResponseEntityResultHandlerTests {
StepVerifier.create(resultHandler.handleResult(exchange, result)) StepVerifier.create(resultHandler.handleResult(exchange, result))
.consumeErrorWith(ex -> assertThat(ex) .consumeErrorWith(ex -> assertThat(ex)
.isInstanceOf(IllegalStateException.class) .isInstanceOf(HttpMessageNotWritableException.class)
.hasMessageContaining("with preset Content-Type")) .hasMessageContaining("with preset Content-Type"))
.verify(); .verify();
} }

View File

@ -176,6 +176,9 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
* @throws IOException thrown in case of I/O errors * @throws IOException thrown in case of I/O errors
* @throws HttpMediaTypeNotAcceptableException thrown when the conditions indicated * @throws HttpMediaTypeNotAcceptableException thrown when the conditions indicated
* by the {@code Accept} header on the request cannot be met by the message converters * by the {@code Accept} header on the request cannot be met by the message converters
* @throws HttpMessageNotWritableException thrown if a given message cannot
* be written by a converter, or if the content-type chosen by the server
* has no compatible converter.
*/ */
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType, protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
@ -306,7 +309,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
if (body != null) { if (body != null) {
if (isContentTypePreset) { if (isContentTypePreset) {
throw new IllegalStateException( throw new HttpMessageNotWritableException(
"No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'"); "No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
} }
throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes); throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);

View File

@ -47,6 +47,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity; import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse; import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.HttpMediaTypeNotAcceptableException;
@ -59,7 +60,7 @@ import static java.time.Instant.ofEpochMilli;
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME; import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
@ -325,10 +326,11 @@ public class HttpEntityMethodProcessorMockTests {
given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true); given(stringHttpMessageConverter.canWrite(String.class, null)).willReturn(true);
given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN)); given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
assertThatIllegalStateException() assertThatThrownBy(() ->
.isThrownBy(() -> processor.handleReturnValue(
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest)) returnValue, returnTypeResponseEntity, mavContainer, webRequest))
.withMessageContaining("with preset Content-Type"); .isInstanceOf(HttpMessageNotWritableException.class)
.hasMessageContaining("with preset Content-Type");
} }
@Test @Test