Skip to content

Commit 81143a8

Browse files
committed
Wrap IOException as HttpMessageNotReadableException for RestTemplate usage
In the 4.3.x line, conversion exceptions do not get wrapped as RestClientException yet, so the expectation remains to receive a HttpMessageNotReadableException for conversion-level IOExceptions. Issue: SPR-13592
1 parent 9572859 commit 81143a8

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessa
219219
return readJavaType(javaType, inputMessage);
220220
}
221221

222-
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) throws IOException {
222+
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
223223
try {
224224
if (inputMessage instanceof MappingJacksonInputMessage) {
225225
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
@@ -233,6 +233,9 @@ private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) th
233233
catch (JsonProcessingException ex) {
234234
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex);
235235
}
236+
catch (IOException ex) {
237+
throw new HttpMessageNotReadableException("I/O error while reading input message", ex);
238+
}
236239
}
237240

238241
@Override

spring-web/src/test/java/org/springframework/http/converter/xml/MappingJackson2XmlHttpMessageConverterTests.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.http.converter.xml;
1818

1919
import java.io.IOException;
20-
import java.lang.reflect.Method;
2120
import java.nio.charset.Charset;
2221

2322
import com.fasterxml.jackson.annotation.JsonView;
@@ -27,11 +26,10 @@
2726
import org.junit.rules.ExpectedException;
2827

2928
import org.springframework.core.io.ClassPathResource;
30-
import org.springframework.http.HttpOutputMessage;
3129
import org.springframework.http.MediaType;
3230
import org.springframework.http.MockHttpInputMessage;
3331
import org.springframework.http.MockHttpOutputMessage;
34-
import org.springframework.http.converter.AbstractHttpMessageConverter;
32+
import org.springframework.http.converter.HttpMessageNotReadableException;
3533
import org.springframework.http.converter.json.MappingJacksonValue;
3634

3735
import static org.hamcrest.CoreMatchers.*;
@@ -102,7 +100,7 @@ public void write() throws IOException {
102100
outputMessage.getHeaders().getContentType());
103101
}
104102

105-
@Test(expected = IOException.class)
103+
@Test(expected = HttpMessageNotReadableException.class)
106104
public void readInvalidXml() throws IOException {
107105
String body = "FooBar";
108106
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
@@ -129,7 +127,7 @@ public void jsonView() throws Exception {
129127

130128
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
131129
jacksonValue.setSerializationView(MyJacksonView1.class);
132-
this.writeInternal(jacksonValue, outputMessage);
130+
this.converter.write(jacksonValue, null, outputMessage);
133131

134132
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
135133
assertThat(result, containsString("<withView1>with</withView1>"));
@@ -154,7 +152,7 @@ public void readWithExternalReference() throws IOException {
154152
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
155153
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
156154

157-
this.thrown.expect(IOException.class);
155+
this.thrown.expect(HttpMessageNotReadableException.class);
158156
this.converter.read(MyBean.class, inputMessage);
159157
}
160158

@@ -181,19 +179,11 @@ public void readWithXmlBomb() throws IOException {
181179
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
182180
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
183181

184-
this.thrown.expect(IOException.class);
182+
this.thrown.expect(HttpMessageNotReadableException.class);
185183
this.converter.read(MyBean.class, inputMessage);
186184
}
187185

188186

189-
private void writeInternal(Object object, HttpOutputMessage outputMessage) throws Exception {
190-
Method method = AbstractHttpMessageConverter.class.getDeclaredMethod(
191-
"writeInternal", Object.class, HttpOutputMessage.class);
192-
method.setAccessible(true);
193-
method.invoke(this.converter, object, outputMessage);
194-
}
195-
196-
197187
public static class MyBean {
198188

199189
private String string;

0 commit comments

Comments
 (0)