From ffd9c62fc8b7cbeb5fbbc9a4fd487b1284a0b264 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 23 Sep 2015 17:22:37 -0400 Subject: [PATCH] Translate IOException to HttpMessageNotReadableEx Some converters (Jackson, Gson, Protobuf) already do this. It is now also done in AbstractMessageConverterMethodArgumentResolver which enforces a consistent behavior across controller method arguments. Issue: SPR-12745 --- ...essageConverterMethodArgumentResolver.java | 72 ++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java index 469a6812f1..d8399ebc3d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java @@ -179,47 +179,53 @@ public abstract class AbstractMessageConverterMethodArgumentResolver implements } HttpMethod httpMethod = ((HttpRequest) inputMessage).getMethod(); - inputMessage = new EmptyBodyCheckingHttpInputMessage(inputMessage); Object body = NO_VALUE; - for (HttpMessageConverter converter : this.messageConverters) { - Class> converterType = (Class>) converter.getClass(); - if (converter instanceof GenericHttpMessageConverter) { - GenericHttpMessageConverter genericConverter = (GenericHttpMessageConverter) converter; - if (genericConverter.canRead(targetType, contextClass, contentType)) { - if (logger.isDebugEnabled()) { - logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]"); - } - if (inputMessage.getBody() != null) { - inputMessage = getAdvice().beforeBodyRead(inputMessage, param, targetType, converterType); - body = genericConverter.read(targetType, contextClass, inputMessage); - body = getAdvice().afterBodyRead(body, inputMessage, param, targetType, converterType); - } - else { - body = null; - body = getAdvice().handleEmptyBody(body, inputMessage, param, targetType, converterType); + try { + inputMessage = new EmptyBodyCheckingHttpInputMessage(inputMessage); + + for (HttpMessageConverter converter : this.messageConverters) { + Class> converterType = (Class>) converter.getClass(); + if (converter instanceof GenericHttpMessageConverter) { + GenericHttpMessageConverter genericConverter = (GenericHttpMessageConverter) converter; + if (genericConverter.canRead(targetType, contextClass, contentType)) { + if (logger.isDebugEnabled()) { + logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]"); + } + if (inputMessage.getBody() != null) { + inputMessage = getAdvice().beforeBodyRead(inputMessage, param, targetType, converterType); + body = genericConverter.read(targetType, contextClass, inputMessage); + body = getAdvice().afterBodyRead(body, inputMessage, param, targetType, converterType); + } + else { + body = null; + body = getAdvice().handleEmptyBody(body, inputMessage, param, targetType, converterType); + } + break; } - break; } - } - else if (targetClass != null) { - if (converter.canRead(targetClass, contentType)) { - if (logger.isDebugEnabled()) { - logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]"); + else if (targetClass != null) { + if (converter.canRead(targetClass, contentType)) { + if (logger.isDebugEnabled()) { + logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]"); + } + if (inputMessage.getBody() != null) { + inputMessage = getAdvice().beforeBodyRead(inputMessage, param, targetType, converterType); + body = ((HttpMessageConverter) converter).read(targetClass, inputMessage); + body = getAdvice().afterBodyRead(body, inputMessage, param, targetType, converterType); + } + else { + body = null; + body = getAdvice().handleEmptyBody(body, inputMessage, param, targetType, converterType); + } + break; } - if (inputMessage.getBody() != null) { - inputMessage = getAdvice().beforeBodyRead(inputMessage, param, targetType, converterType); - body = ((HttpMessageConverter) converter).read(targetClass, inputMessage); - body = getAdvice().afterBodyRead(body, inputMessage, param, targetType, converterType); - } - else { - body = null; - body = getAdvice().handleEmptyBody(body, inputMessage, param, targetType, converterType); - } - break; } } } + catch (IOException ex) { + throw new HttpMessageNotReadableException("Could not read document: " + ex.getMessage(), ex); + } if (body == NO_VALUE) { if (!SUPPORTED_METHODS.contains(httpMethod) -- GitLab