- Javadoc
- Renamed JacksonHttpMessageConverter to BindingJacksonHttpMessageConverter
This commit is contained in:
parent
d26df490b5
commit
9a84ef4686
|
|
@ -18,6 +18,7 @@ package org.springframework.http.converter.json;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import org.codehaus.jackson.JsonEncoding;
|
||||
import org.codehaus.jackson.JsonFactory;
|
||||
|
|
@ -34,10 +35,19 @@ import org.springframework.http.converter.HttpMessageNotWritableException;
|
|||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that can read
|
||||
* and write JSON using <a href="http://jackson.codehaus.org/">Jackson's</a> {@link ObjectMapper}.
|
||||
*
|
||||
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances.
|
||||
*
|
||||
* <p>By default, this converter supports {@code application/json}. This can be overridden by setting the {@link
|
||||
* #setSupportedMediaTypes(List) supportedMediaTypes} property, and overriding the {@link #getContentType(Object)}
|
||||
* method.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
*/
|
||||
public class JacksonHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> {
|
||||
public class BindingJacksonHttpMessageConverter<T> extends AbstractHttpMessageConverter<T> {
|
||||
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
|
|
@ -45,20 +55,31 @@ public class JacksonHttpMessageConverter<T> extends AbstractHttpMessageConverter
|
|||
|
||||
private JsonEncoding encoding = JsonEncoding.UTF8;
|
||||
|
||||
public JacksonHttpMessageConverter() {
|
||||
/**
|
||||
* Construct a new {@code BindingJacksonHttpMessageConverter},
|
||||
*/
|
||||
public BindingJacksonHttpMessageConverter() {
|
||||
super(new MediaType("application", "json"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@code ObjectMapper} for this converter. By default, a default {@link ObjectMapper#ObjectMapper()
|
||||
* ObjectMapper} is used.
|
||||
*/
|
||||
public void setObjectMapper(ObjectMapper objectMapper) {
|
||||
Assert.notNull(objectMapper, "'objectMapper' must not be null");
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/** Sets the {@code JsonFactory} for this converter. By default, a {@link MappingJsonFactory} is used. */
|
||||
public void setJsonFactory(JsonFactory jsonFactory) {
|
||||
Assert.notNull(jsonFactory, "'jsonFactory' must not be null");
|
||||
this.jsonFactory = jsonFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@code JsonEncoding} for this converter. By default, {@linkplain JsonEncoding#UTF8 UTF-8} is used.
|
||||
*/
|
||||
public void setEncoding(JsonEncoding encoding) {
|
||||
this.encoding = encoding;
|
||||
}
|
||||
|
|
@ -44,7 +44,6 @@ import org.springframework.util.Assert;
|
|||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
*/
|
||||
|
||||
public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConverter<Object>
|
||||
implements InitializingBean {
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,9 @@ package org.springframework.http.converter.json;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
|
|
@ -28,17 +31,22 @@ import org.springframework.http.MockHttpInputMessage;
|
|||
import org.springframework.http.MockHttpOutputMessage;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class JacksonHttpMessageConverterTest {
|
||||
public class BindingJacksonHttpMessageConverterTest {
|
||||
|
||||
private JacksonHttpMessageConverter<MyBean> converter;
|
||||
private BindingJacksonHttpMessageConverter<MyBean> converter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
converter = new JacksonHttpMessageConverter<MyBean>();
|
||||
converter = new BindingJacksonHttpMessageConverter<MyBean>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supports() {
|
||||
assertTrue(converter.supports(MyBean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void read() throws IOException {
|
||||
public void readTyped() throws IOException {
|
||||
String body =
|
||||
"{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
|
||||
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
|
||||
|
|
@ -52,6 +60,26 @@ public class JacksonHttpMessageConverterTest {
|
|||
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public void readUntyped() throws IOException {
|
||||
BindingJacksonHttpMessageConverter<HashMap> converter = new BindingJacksonHttpMessageConverter<HashMap>();
|
||||
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"));
|
||||
HashMap<String,Object> result = converter.read(HashMap.class, inputMessage);
|
||||
assertEquals("Foo", result.get("string"));
|
||||
assertEquals(42, result.get("number"));
|
||||
assertEquals(42D, (Double)result.get("fraction"), 0D);
|
||||
List array = new ArrayList();
|
||||
array.add("Foo");
|
||||
array.add("Bar");
|
||||
assertEquals(array, result.get("array"));
|
||||
assertEquals(Boolean.TRUE, result.get("bool"));
|
||||
assertEquals("AQI=", result.get("bytes"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void write() throws IOException {
|
||||
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
|
||||
Loading…
Reference in New Issue