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
This commit is contained in:
Rossen Stoyanchev
2017-05-05 14:44:08 -04:00
parent 83e0e1604a
commit 4d962a1793
6 changed files with 27 additions and 51 deletions

View File

@@ -17,9 +17,6 @@
package org.springframework.http.converter;
import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpStatus;
import java.util.Optional;
/**
* Thrown by {@link HttpMessageConverter} implementations when a conversion attempt fails.
@@ -31,15 +28,12 @@ import java.util.Optional;
@SuppressWarnings("serial")
public class HttpMessageConversionException extends NestedRuntimeException {
private final HttpStatus errorStatus;
/**
* Create a new HttpMessageConversionException.
* @param msg the detail message
*/
public HttpMessageConversionException(String msg) {
super(msg);
this.errorStatus = null;
}
/**
@@ -49,25 +43,6 @@ public class HttpMessageConversionException extends NestedRuntimeException {
*/
public HttpMessageConversionException(String msg, Throwable cause) {
super(msg, cause);
this.errorStatus = null;
}
/**
* Create a new HttpMessageConversionException.
* @since 5.0
* @param msg the detail message
* @param cause the root cause (if any)
* @param errorStatus the HTTP error status related to this exception
*/
public HttpMessageConversionException(String msg, Throwable cause, HttpStatus errorStatus) {
super(msg, cause);
this.errorStatus = errorStatus;
}
/**
* Return the HTTP error status related to this exception if any.
*/
public Optional<HttpStatus> getErrorStatus() {
return Optional.ofNullable(errorStatus);
}
}

View File

@@ -16,8 +16,6 @@
package org.springframework.http.converter;
import org.springframework.http.HttpStatus;
/**
* Thrown by {@link HttpMessageConverter} implementations when the
* {@link HttpMessageConverter#read} method fails.
@@ -45,15 +43,4 @@ public class HttpMessageNotReadableException extends HttpMessageConversionExcept
super(msg, cause);
}
/**
* Create a new HttpMessageNotReadableException.
* @since 5.0
* @param msg the detail message
* @param cause the root cause (if any)
* @param errorStatus the HTTP error status related to this exception
*/
public HttpMessageNotReadableException(String msg, Throwable cause, HttpStatus errorStatus) {
super(msg, cause, errorStatus);
}
}

View File

@@ -41,9 +41,9 @@ import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.core.GenericTypeResolver;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractGenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
@@ -230,11 +230,13 @@ public abstract class AbstractJackson2HttpMessageConverter extends AbstractGener
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
}
catch (InvalidDefinitionException ex) {
throw new HttpMessageNotReadableException(
"Could not map JSON to target object of " + javaType, ex, HttpStatus.INTERNAL_SERVER_ERROR);
throw new HttpMessageConversionException("Type definition error: " + ex.getMessage(), ex);
}
catch (JsonProcessingException ex) {
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getMessage(), ex);
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("Could not read JSON document: " + ex.getMessage(), ex);
throw new HttpMessageNotReadableException("I/O error while reading: " + ex.getMessage(), ex);
}
}