|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2013 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. |
|
|
|
@ -19,7 +19,6 @@ package org.springframework.http.converter.json; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.lang.reflect.Type; |
|
|
|
import java.lang.reflect.Type; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonEncoding; |
|
|
|
import com.fasterxml.jackson.core.JsonEncoding; |
|
|
|
import com.fasterxml.jackson.core.JsonGenerator; |
|
|
|
import com.fasterxml.jackson.core.JsonGenerator; |
|
|
|
@ -45,13 +44,14 @@ import org.springframework.util.Assert; |
|
|
|
* <p>This converter can be used to bind to typed beans, or untyped {@link java.util.HashMap HashMap} instances. |
|
|
|
* <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 |
|
|
|
* <p>By default, this converter supports {@code application/json}. This can be overridden by setting the |
|
|
|
* {@link #setSupportedMediaTypes(List) supportedMediaTypes} property. |
|
|
|
* {@link #setSupportedMediaTypes supportedMediaTypes} property. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* <p>Tested against Jackson 2.2; compatible with Jackson 2.0 and higher. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Keith Donald |
|
|
|
* @author Keith Donald |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @since 3.1.2 |
|
|
|
* @since 3.1.2 |
|
|
|
* @see org.springframework.web.servlet.view.json.MappingJackson2JsonView |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object> |
|
|
|
public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConverter<Object> |
|
|
|
implements GenericHttpMessageConverter<Object> { |
|
|
|
implements GenericHttpMessageConverter<Object> { |
|
|
|
@ -70,12 +70,14 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv |
|
|
|
* Construct a new {@code MappingJackson2HttpMessageConverter}. |
|
|
|
* Construct a new {@code MappingJackson2HttpMessageConverter}. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public MappingJackson2HttpMessageConverter() { |
|
|
|
public MappingJackson2HttpMessageConverter() { |
|
|
|
super(new MediaType("application", "json", DEFAULT_CHARSET), new MediaType("application", "*+json", DEFAULT_CHARSET)); |
|
|
|
super(new MediaType("application", "json", DEFAULT_CHARSET), |
|
|
|
|
|
|
|
new MediaType("application", "*+json", DEFAULT_CHARSET)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the {@code ObjectMapper} for this view. If not set, a default |
|
|
|
* Set the {@code ObjectMapper} for this view. |
|
|
|
* {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. |
|
|
|
* If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. |
|
|
|
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON |
|
|
|
* <p>Setting a custom-configured {@code ObjectMapper} is one way to take further control of the JSON |
|
|
|
* serialization process. For example, an extended {@link org.codehaus.jackson.map.SerializerFactory} |
|
|
|
* serialization process. For example, an extended {@link org.codehaus.jackson.map.SerializerFactory} |
|
|
|
* can be configured that provides custom serializers for specific types. The other option for refining |
|
|
|
* can be configured that provides custom serializers for specific types. The other option for refining |
|
|
|
@ -88,12 +90,6 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv |
|
|
|
configurePrettyPrint(); |
|
|
|
configurePrettyPrint(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void configurePrettyPrint() { |
|
|
|
|
|
|
|
if (this.prettyPrint != null) { |
|
|
|
|
|
|
|
this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, this.prettyPrint); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Return the underlying {@code ObjectMapper} for this view. |
|
|
|
* Return the underlying {@code ObjectMapper} for this view. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -126,6 +122,12 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv |
|
|
|
configurePrettyPrint(); |
|
|
|
configurePrettyPrint(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void configurePrettyPrint() { |
|
|
|
|
|
|
|
if (this.prettyPrint != null) { |
|
|
|
|
|
|
|
this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, this.prettyPrint); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean canRead(Class<?> clazz, MediaType mediaType) { |
|
|
|
public boolean canRead(Class<?> clazz, MediaType mediaType) { |
|
|
|
@ -172,7 +174,6 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void writeInternal(Object object, HttpOutputMessage outputMessage) |
|
|
|
protected void writeInternal(Object object, HttpOutputMessage outputMessage) |
|
|
|
throws IOException, HttpMessageNotWritableException { |
|
|
|
throws IOException, HttpMessageNotWritableException { |
|
|
|
@ -180,6 +181,7 @@ public class MappingJackson2HttpMessageConverter extends AbstractHttpMessageConv |
|
|
|
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); |
|
|
|
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); |
|
|
|
// The following has been deprecated as late as Jackson 2.2 (April 2013);
|
|
|
|
// The following has been deprecated as late as Jackson 2.2 (April 2013);
|
|
|
|
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
|
|
|
|
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
JsonGenerator jsonGenerator = |
|
|
|
JsonGenerator jsonGenerator = |
|
|
|
this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding); |
|
|
|
this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding); |
|
|
|
|
|
|
|
|
|
|
|
|