SPR-7002 - RestTemplate fails to convert properly for Generic Type Container with MappingJacksonHttpMessageConverter
This commit is contained in:
@@ -23,6 +23,8 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
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;
|
||||
@@ -56,7 +58,6 @@ public class MappingJacksonHttpMessageConverterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readTyped() throws IOException {
|
||||
String body =
|
||||
"{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
|
||||
@@ -71,6 +72,36 @@ public class MappingJacksonHttpMessageConverterTests {
|
||||
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readGenerics() throws IOException {
|
||||
converter = new MappingJacksonHttpMessageConverter() {
|
||||
@Override
|
||||
protected JavaType getJavaType(Class<?> clazz) {
|
||||
if (List.class.isAssignableFrom(clazz)) {
|
||||
return TypeFactory.collectionType(ArrayList.class, MyBean.class);
|
||||
}
|
||||
else {
|
||||
return super.getJavaType(clazz);
|
||||
}
|
||||
}
|
||||
};
|
||||
String body =
|
||||
"[{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
|
||||
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
|
||||
|
||||
List<MyBean> results = (List<MyBean>) converter.read(List.class, inputMessage);
|
||||
assertEquals(1, results.size());
|
||||
MyBean result = results.get(0);
|
||||
assertEquals("Foo", result.getString());
|
||||
assertEquals(42, result.getNumber());
|
||||
assertEquals(42F, result.getFraction(), 0F);
|
||||
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray());
|
||||
assertTrue(result.isBool());
|
||||
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readUntyped() throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user