Skip to content

Commit 4d962a1

Browse files
committed
Remove HttpStatus from HttpMessageConversionException
HttpMessageConverter's are client and server and arguably shouldn't contain a server-side concept such a response status. The status field is recent, it was added to differentiate 400 vs 500 errors with Jackson 2.9+ but there is no need for it since the same distinction is reflected in raising an HttpMessageNotReadableException vs a general HttpMessageConversionException. Issue: SPR-15516
1 parent 83e0e16 commit 4d962a1

File tree

6 files changed

+27
-51
lines changed

6 files changed

+27
-51
lines changed

spring-web/src/main/java/org/springframework/http/converter/HttpMessageConversionException.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
package org.springframework.http.converter;
1818

1919
import org.springframework.core.NestedRuntimeException;
20-
import org.springframework.http.HttpStatus;
21-
22-
import java.util.Optional;
2320

2421
/**
2522
* Thrown by {@link HttpMessageConverter} implementations when a conversion attempt fails.
@@ -31,15 +28,12 @@
3128
@SuppressWarnings("serial")
3229
public class HttpMessageConversionException extends NestedRuntimeException {
3330

34-
private final HttpStatus errorStatus;
35-
3631
/**
3732
* Create a new HttpMessageConversionException.
3833
* @param msg the detail message
3934
*/
4035
public HttpMessageConversionException(String msg) {
4136
super(msg);
42-
this.errorStatus = null;
4337
}
4438

4539
/**
@@ -49,25 +43,6 @@ public HttpMessageConversionException(String msg) {
4943
*/
5044
public HttpMessageConversionException(String msg, Throwable cause) {
5145
super(msg, cause);
52-
this.errorStatus = null;
5346
}
5447

55-
/**
56-
* Create a new HttpMessageConversionException.
57-
* @since 5.0
58-
* @param msg the detail message
59-
* @param cause the root cause (if any)
60-
* @param errorStatus the HTTP error status related to this exception
61-
*/
62-
public HttpMessageConversionException(String msg, Throwable cause, HttpStatus errorStatus) {
63-
super(msg, cause);
64-
this.errorStatus = errorStatus;
65-
}
66-
67-
/**
68-
* Return the HTTP error status related to this exception if any.
69-
*/
70-
public Optional<HttpStatus> getErrorStatus() {
71-
return Optional.ofNullable(errorStatus);
72-
}
7348
}

spring-web/src/main/java/org/springframework/http/converter/HttpMessageNotReadableException.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.http.converter;
1818

19-
import org.springframework.http.HttpStatus;
20-
2119
/**
2220
* Thrown by {@link HttpMessageConverter} implementations when the
2321
* {@link HttpMessageConverter#read} method fails.
@@ -45,15 +43,4 @@ public HttpMessageNotReadableException(String msg, Throwable cause) {
4543
super(msg, cause);
4644
}
4745

48-
/**
49-
* Create a new HttpMessageNotReadableException.
50-
* @since 5.0
51-
* @param msg the detail message
52-
* @param cause the root cause (if any)
53-
* @param errorStatus the HTTP error status related to this exception
54-
*/
55-
public HttpMessageNotReadableException(String msg, Throwable cause, HttpStatus errorStatus) {
56-
super(msg, cause, errorStatus);
57-
}
58-
5946
}

spring-web/src/main/java/org/springframework/http/converter/json/AbstractJackson2HttpMessageConverter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
import org.springframework.core.GenericTypeResolver;
4242
import org.springframework.http.HttpInputMessage;
4343
import org.springframework.http.HttpOutputMessage;
44-
import org.springframework.http.HttpStatus;
4544
import org.springframework.http.MediaType;
4645
import org.springframework.http.converter.AbstractGenericHttpMessageConverter;
46+
import org.springframework.http.converter.HttpMessageConversionException;
4747
import org.springframework.http.converter.HttpMessageConverter;
4848
import org.springframework.http.converter.HttpMessageNotReadableException;
4949
import org.springframework.http.converter.HttpMessageNotWritableException;
@@ -230,11 +230,13 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
230230
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
231231
}
232232
catch (InvalidDefinitionException ex) {
233-
throw new HttpMessageNotReadableException(
234-
"Could not map JSON to target object of " + javaType, ex, HttpStatus.INTERNAL_SERVER_ERROR);
233+
throw new HttpMessageConversionException("Type definition error: " + ex.getMessage(), ex);
234+
}
235+
catch (JsonProcessingException ex) {
236+
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getMessage(), ex);
235237
}
236238
catch (IOException ex) {
237-
throw new HttpMessageNotReadableException("Could not read JSON document: " + ex.getMessage(), ex);
239+
throw new HttpMessageNotReadableException("I/O error while reading: " + ex.getMessage(), ex);
238240
}
239241
}
240242

spring-web/src/test/java/org/springframework/http/converter/json/MappingJackson2HttpMessageConverterTests.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,21 @@
3434
import org.junit.Test;
3535

3636
import org.springframework.core.ParameterizedTypeReference;
37-
import org.springframework.http.HttpStatus;
3837
import org.springframework.http.MediaType;
3938
import org.springframework.http.MockHttpInputMessage;
4039
import org.springframework.http.MockHttpOutputMessage;
40+
import org.springframework.http.converter.HttpMessageConversionException;
4141
import org.springframework.http.converter.HttpMessageNotReadableException;
4242

43-
import static org.hamcrest.CoreMatchers.*;
44-
import static org.junit.Assert.*;
43+
import static org.hamcrest.CoreMatchers.containsString;
44+
import static org.hamcrest.CoreMatchers.endsWith;
45+
import static org.hamcrest.CoreMatchers.not;
46+
import static org.hamcrest.CoreMatchers.startsWith;
47+
import static org.junit.Assert.assertArrayEquals;
48+
import static org.junit.Assert.assertEquals;
49+
import static org.junit.Assert.assertThat;
50+
import static org.junit.Assert.assertTrue;
51+
import static org.junit.Assert.fail;
4552

4653
/**
4754
* Jackson 2.x converter tests.
@@ -388,9 +395,8 @@ public void readWithNoDefaultConstructor() throws Exception {
388395
try {
389396
converter.read(BeanWithNoDefaultConstructor.class, inputMessage);
390397
}
391-
catch (HttpMessageNotReadableException ex) {
392-
assertTrue(ex.getErrorStatus().isPresent());
393-
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getErrorStatus().get());
398+
catch (HttpMessageConversionException ex) {
399+
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Type definition error:"));
394400
return;
395401
}
396402
fail();

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.beans.ConversionNotSupportedException;
2828
import org.springframework.beans.TypeMismatchException;
2929
import org.springframework.core.Ordered;
30-
import org.springframework.http.HttpStatus;
3130
import org.springframework.http.MediaType;
3231
import org.springframework.http.converter.HttpMessageNotReadableException;
3332
import org.springframework.http.converter.HttpMessageNotWritableException;
@@ -355,7 +354,7 @@ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableExcept
355354
if (logger.isWarnEnabled()) {
356355
logger.warn("Failed to read HTTP message: " + ex);
357356
}
358-
response.sendError(ex.getErrorStatus().orElse(HttpStatus.BAD_REQUEST).value());
357+
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
359358
return new ModelAndView();
360359
}
361360

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@
141141
import org.springframework.web.servlet.support.RequestContextUtils;
142142
import org.springframework.web.servlet.view.InternalResourceViewResolver;
143143

144-
import static org.junit.Assert.*;
144+
import static org.junit.Assert.assertArrayEquals;
145+
import static org.junit.Assert.assertEquals;
146+
import static org.junit.Assert.assertFalse;
147+
import static org.junit.Assert.assertNotNull;
148+
import static org.junit.Assert.assertNull;
149+
import static org.junit.Assert.assertSame;
150+
import static org.junit.Assert.assertTrue;
151+
import static org.junit.Assert.fail;
145152

146153
/**
147154
* @author Rossen Stoyanchev

0 commit comments

Comments
 (0)