|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* Copyright 2002-2018 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -32,6 +32,7 @@ import com.fasterxml.jackson.databind.ser.FilterProvider; |
|
|
|
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
|
|
|
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
|
|
|
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; |
|
|
|
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import org.skyscreamer.jsonassert.JSONAssert; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.ParameterizedTypeReference; |
|
|
|
import org.springframework.core.ParameterizedTypeReference; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
@ -41,21 +42,15 @@ import org.springframework.http.converter.HttpMessageConversionException; |
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException; |
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException; |
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.containsString; |
|
|
|
import static org.hamcrest.CoreMatchers.*; |
|
|
|
import static org.hamcrest.CoreMatchers.endsWith; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.hamcrest.CoreMatchers.not; |
|
|
|
|
|
|
|
import static org.hamcrest.CoreMatchers.startsWith; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertArrayEquals; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertThat; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
import static org.junit.Assert.fail; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Jackson 2.x converter tests. |
|
|
|
* Jackson 2.x converter tests. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Sebastien Deleuze |
|
|
|
* @author Sebastien Deleuze |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MappingJackson2HttpMessageConverterTests { |
|
|
|
public class MappingJackson2HttpMessageConverterTests { |
|
|
|
|
|
|
|
|
|
|
|
@ -97,9 +92,9 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray()); |
|
|
|
assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes()); |
|
|
|
assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -133,9 +128,9 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
body.setString("Foo"); |
|
|
|
body.setString("Foo"); |
|
|
|
body.setNumber(42); |
|
|
|
body.setNumber(42); |
|
|
|
body.setFraction(42F); |
|
|
|
body.setFraction(42F); |
|
|
|
body.setArray(new String[]{"Foo", "Bar"}); |
|
|
|
body.setArray(new String[] {"Foo", "Bar"}); |
|
|
|
body.setBool(true); |
|
|
|
body.setBool(true); |
|
|
|
body.setBytes(new byte[]{0x1, 0x2}); |
|
|
|
body.setBytes(new byte[] {0x1, 0x2}); |
|
|
|
converter.write(body, null, outputMessage); |
|
|
|
converter.write(body, null, outputMessage); |
|
|
|
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); |
|
|
|
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); |
|
|
|
assertTrue(result.contains("\"string\":\"Foo\"")); |
|
|
|
assertTrue(result.contains("\"string\":\"Foo\"")); |
|
|
|
@ -148,6 +143,28 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
outputMessage.getHeaders().getContentType()); |
|
|
|
outputMessage.getHeaders().getContentType()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void writeWithBaseType() throws IOException { |
|
|
|
|
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
|
|
|
|
MyBean body = new MyBean(); |
|
|
|
|
|
|
|
body.setString("Foo"); |
|
|
|
|
|
|
|
body.setNumber(42); |
|
|
|
|
|
|
|
body.setFraction(42F); |
|
|
|
|
|
|
|
body.setArray(new String[] {"Foo", "Bar"}); |
|
|
|
|
|
|
|
body.setBool(true); |
|
|
|
|
|
|
|
body.setBytes(new byte[] {0x1, 0x2}); |
|
|
|
|
|
|
|
converter.write(body, MyBase.class, null, outputMessage); |
|
|
|
|
|
|
|
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
assertTrue(result.contains("\"string\":\"Foo\"")); |
|
|
|
|
|
|
|
assertTrue(result.contains("\"number\":42")); |
|
|
|
|
|
|
|
assertTrue(result.contains("fraction\":42.0")); |
|
|
|
|
|
|
|
assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]")); |
|
|
|
|
|
|
|
assertTrue(result.contains("\"bool\":true")); |
|
|
|
|
|
|
|
assertTrue(result.contains("\"bytes\":\"AQI=\"")); |
|
|
|
|
|
|
|
assertEquals("Invalid content-type", new MediaType("application", "json", StandardCharsets.UTF_8), |
|
|
|
|
|
|
|
outputMessage.getHeaders().getContentType()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void writeUTF16() throws IOException { |
|
|
|
public void writeUTF16() throws IOException { |
|
|
|
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE); |
|
|
|
MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE); |
|
|
|
@ -177,7 +194,7 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public void readGenerics() throws IOException { |
|
|
|
public void readAndWriteGenerics() throws Exception { |
|
|
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter() { |
|
|
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) { |
|
|
|
protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) { |
|
|
|
@ -205,14 +222,18 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray()); |
|
|
|
assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes()); |
|
|
|
assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
|
|
|
|
converter.write(results, new MediaType("application", "json"), outputMessage); |
|
|
|
|
|
|
|
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public void readParameterizedType() throws IOException { |
|
|
|
public void readAndWriteParameterizedType() throws Exception { |
|
|
|
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {}; |
|
|
|
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {}; |
|
|
|
|
|
|
|
|
|
|
|
String body = "[{" + |
|
|
|
String body = "[{" + |
|
|
|
@ -232,11 +253,46 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals("Foo", result.getString()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42, result.getNumber()); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertEquals(42F, result.getFraction(), 0F); |
|
|
|
assertArrayEquals(new String[]{"Foo", "Bar"}, result.getArray()); |
|
|
|
assertArrayEquals(new String[] {"Foo", "Bar"}, result.getArray()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertTrue(result.isBool()); |
|
|
|
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes()); |
|
|
|
assertArrayEquals(new byte[] {0x1, 0x2}, result.getBytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
|
|
|
|
converter.write(results, beansList.getType(), new MediaType("application", "json"), outputMessage); |
|
|
|
|
|
|
|
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
|
|
public void writeParameterizedBaseType() throws Exception { |
|
|
|
|
|
|
|
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {}; |
|
|
|
|
|
|
|
ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<List<MyBase>>() {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); |
|
|
|
|
|
|
|
List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, 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()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
|
|
|
|
converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage); |
|
|
|
|
|
|
|
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void prettyPrint() throws Exception { |
|
|
|
public void prettyPrint() throws Exception { |
|
|
|
@ -433,10 +489,22 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class MyBean implements MyInterface { |
|
|
|
public static class MyBase implements MyInterface{ |
|
|
|
|
|
|
|
|
|
|
|
private String string; |
|
|
|
private String string; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getString() { |
|
|
|
|
|
|
|
return string; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setString(String string) { |
|
|
|
|
|
|
|
this.string = string; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class MyBean extends MyBase { |
|
|
|
|
|
|
|
|
|
|
|
private int number; |
|
|
|
private int number; |
|
|
|
|
|
|
|
|
|
|
|
private float fraction; |
|
|
|
private float fraction; |
|
|
|
@ -447,30 +515,6 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
|
|
|
|
|
|
|
|
private byte[] bytes; |
|
|
|
private byte[] bytes; |
|
|
|
|
|
|
|
|
|
|
|
public byte[] getBytes() { |
|
|
|
|
|
|
|
return bytes; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setBytes(byte[] bytes) { |
|
|
|
|
|
|
|
this.bytes = bytes; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isBool() { |
|
|
|
|
|
|
|
return bool; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setBool(boolean bool) { |
|
|
|
|
|
|
|
this.bool = bool; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getString() { |
|
|
|
|
|
|
|
return string; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setString(String string) { |
|
|
|
|
|
|
|
this.string = string; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int getNumber() { |
|
|
|
public int getNumber() { |
|
|
|
return number; |
|
|
|
return number; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -494,6 +538,22 @@ public class MappingJackson2HttpMessageConverterTests { |
|
|
|
public void setArray(String[] array) { |
|
|
|
public void setArray(String[] array) { |
|
|
|
this.array = array; |
|
|
|
this.array = array; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isBool() { |
|
|
|
|
|
|
|
return bool; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setBool(boolean bool) { |
|
|
|
|
|
|
|
this.bool = bool; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] getBytes() { |
|
|
|
|
|
|
|
return bytes; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setBytes(byte[] bytes) { |
|
|
|
|
|
|
|
this.bytes = bytes; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|