use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class RequestEntityTests method types.
@Test
// SPR-13154
void types() throws URISyntaxException {
URI url = new URI("https://example.com");
List<String> body = Arrays.asList("foo", "bar");
TypeReference<?> typeReference = new TypeReference<List<String>>() {
};
RequestEntity<?> entity = RequestEntity.post(url).body(body, typeReference.getType());
assertThat(entity.getType()).isEqualTo(typeReference.getType());
}
use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class MappingJackson2HttpMessageConverterTests method writeSubTypeList.
// SPR-13318
@Test
public void writeSubTypeList() throws Exception {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
List<MyBean> beans = new ArrayList<>();
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);
TypeReference<List<MyInterface>> typeReference = new TypeReference<List<MyInterface>>() {
};
this.converter.writeInternal(beans, typeReference.getType(), outputMessage);
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
assertThat(result.contains("\"string\":\"Foo\"")).isTrue();
assertThat(result.contains("\"number\":42")).isTrue();
assertThat(result.contains("\"string\":\"Bar\"")).isTrue();
assertThat(result.contains("\"number\":123")).isTrue();
}
use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class GsonHttpMessageConverterTests method readAndWriteParameterizedType.
@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
TypeReference<List<MyBean>> beansList = new TypeReference<List<MyBean>>() {
};
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
assertThat(results.size()).isEqualTo(1);
MyBean result = results.get(0);
assertThat(result.getString()).isEqualTo("Foo");
assertThat(result.getNumber()).isEqualTo(42);
assertThat(result.getFraction()).isCloseTo(42F, within(0F));
assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
assertThat(result.isBool()).isTrue();
assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(results, beansList.getType(), new MediaType("application", "json"), outputMessage);
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class GsonHttpMessageConverterTests method writeParameterizedBaseType.
@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
TypeReference<List<MyBean>> beansList = new TypeReference<List<MyBean>>() {
};
TypeReference<List<MyBase>> baseList = new TypeReference<List<MyBase>>() {
};
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
assertThat(results.size()).isEqualTo(1);
MyBean result = results.get(0);
assertThat(result.getString()).isEqualTo("Foo");
assertThat(result.getNumber()).isEqualTo(42);
assertThat(result.getFraction()).isCloseTo(42F, within(0F));
assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
assertThat(result.isBool()).isTrue();
assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage);
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class JsonbHttpMessageConverterTests method writeParameterizedBaseType.
@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
TypeReference<List<MyBean>> beansList = new TypeReference<List<MyBean>>() {
};
TypeReference<List<MyBase>> baseList = new TypeReference<List<MyBase>>() {
};
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
assertThat(results.size()).isEqualTo(1);
MyBean result = results.get(0);
assertThat(result.getString()).isEqualTo("Foo");
assertThat(result.getNumber()).isEqualTo(42);
assertThat(result.getFraction()).isCloseTo(42F, within(0F));
assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
assertThat(result.isBool()).isTrue();
assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage);
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Aggregations