Browse Source

SPR-8476 Translate JsonMappingExceptions to HttpMessageNotReadableException (and thus response status code 400) consistent with JsonParseExceptions

pull/7/head
Rossen Stoyanchev 14 years ago
parent
commit
efaa941672
  1. 4
      org.springframework.web/src/main/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java
  2. 16
      org.springframework.web/src/test/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverterTests.java

4
org.springframework.web/src/main/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java

@ -25,6 +25,7 @@ import org.codehaus.jackson.JsonEncoding; @@ -25,6 +25,7 @@ import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;
@ -138,6 +139,9 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve @@ -138,6 +139,9 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
catch (JsonParseException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}
catch (JsonMappingException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}
catch (EOFException ex) {
throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
}

16
org.springframework.web/src/test/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverterTests.java

@ -16,6 +16,10 @@ @@ -16,6 +16,10 @@
package org.springframework.http.converter.json;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
@ -25,10 +29,8 @@ import java.util.Map; @@ -25,10 +29,8 @@ import java.util.Map;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
@ -114,7 +116,7 @@ public class MappingJacksonHttpMessageConverterTests { @@ -114,7 +116,7 @@ public class MappingJacksonHttpMessageConverterTests {
assertEquals("Foo", result.get("string"));
assertEquals(42, result.get("number"));
assertEquals(42D, (Double) result.get("fraction"), 0D);
List array = new ArrayList();
List<String> array = new ArrayList<String>();
array.add("Foo");
array.add("Bar");
assertEquals(array, result.get("array"));
@ -164,7 +166,13 @@ public class MappingJacksonHttpMessageConverterTests { @@ -164,7 +166,13 @@ public class MappingJacksonHttpMessageConverterTests {
converter.read(MyBean.class, inputMessage);
}
@Test(expected = HttpMessageNotReadableException.class)
public void readValidJsonWithUnknownProperty() throws IOException {
String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
converter.read(MyBean.class, inputMessage);
}
public static class MyBean {

Loading…
Cancel
Save