Serialize with type only collections in Jackson HttpMessageConverter
Issue: SPR-13318
This commit is contained in:
@@ -315,8 +315,54 @@ public class MappingJackson2HttpMessageConverterTests {
|
||||
assertThat(result, not(containsString("\"withoutView\":\"without\"")));
|
||||
}
|
||||
|
||||
@Test // SPR-13318
|
||||
public void writeSubType() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
MyBean bean = new MyBean();
|
||||
bean.setString("Foo");
|
||||
bean.setNumber(42);
|
||||
|
||||
public static class MyBean {
|
||||
this.converter.writeInternal(bean, MyInterface.class, outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
assertTrue(result.contains("\"string\":\"Foo\""));
|
||||
assertTrue(result.contains("\"number\":42"));
|
||||
}
|
||||
|
||||
@Test // SPR-13318
|
||||
public void writeSubTypeList() throws Exception {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
List<MyBean> beans = new ArrayList<MyBean>();
|
||||
MyBean foo = new MyBean();
|
||||
foo.setString("Foo");
|
||||
foo.setNumber(42);
|
||||
beans.add(foo);
|
||||
MyBean bar = new MyBean();
|
||||
bar.setString("Bar");
|
||||
bar.setNumber(123);
|
||||
beans.add(bar);
|
||||
ParameterizedTypeReference<List<MyInterface>> typeReference =
|
||||
new ParameterizedTypeReference<List<MyInterface>>() {};
|
||||
|
||||
this.converter.writeInternal(beans, typeReference.getType(), outputMessage);
|
||||
|
||||
String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
|
||||
assertTrue(result.contains("\"string\":\"Foo\""));
|
||||
assertTrue(result.contains("\"number\":42"));
|
||||
assertTrue(result.contains("\"string\":\"Bar\""));
|
||||
assertTrue(result.contains("\"number\":123"));
|
||||
}
|
||||
|
||||
|
||||
interface MyInterface {
|
||||
|
||||
String getString();
|
||||
|
||||
void setString(String string);
|
||||
}
|
||||
|
||||
|
||||
public static class MyBean implements MyInterface {
|
||||
|
||||
private String string;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user