public class MappingJackson2HttpMessageConverter
extends com.fr.third.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
implements com.fr.third.springframework.http.converter.GenericHttpMessageConverter<java.lang.Object>
限定符和类型 | 字段和说明 |
---|---|
static java.nio.charset.Charset |
DEFAULT_CHARSET |
构造器和说明 |
---|
MappingJackson2HttpMessageConverter()
Construct a new
MappingJackson2HttpMessageConverter . |
限定符和类型 | 方法和说明 |
---|---|
boolean |
canRead(java.lang.Class<?> clazz,
com.fr.third.springframework.http.MediaType mediaType) |
boolean |
canRead(java.lang.reflect.Type type,
java.lang.Class<?> contextClass,
com.fr.third.springframework.http.MediaType mediaType) |
boolean |
canWrite(java.lang.Class<?> clazz,
com.fr.third.springframework.http.MediaType mediaType) |
protected com.fr.third.fasterxml.jackson.databind.JavaType |
getJavaType(java.lang.reflect.Type type,
java.lang.Class<?> contextClass)
Return the Jackson
JavaType for the specified type and context class. |
protected com.fr.third.fasterxml.jackson.core.JsonEncoding |
getJsonEncoding(com.fr.third.springframework.http.MediaType contentType)
Determine the JSON encoding to use for the given content type.
|
com.fr.third.fasterxml.jackson.databind.ObjectMapper |
getObjectMapper()
Return the underlying
ObjectMapper for this view. |
java.lang.Object |
read(java.lang.reflect.Type type,
java.lang.Class<?> contextClass,
com.fr.third.springframework.http.HttpInputMessage inputMessage) |
protected java.lang.Object |
readInternal(java.lang.Class<?> clazz,
com.fr.third.springframework.http.HttpInputMessage inputMessage) |
void |
setJsonPrefix(java.lang.String jsonPrefix)
Specify a custom prefix to use for this view's JSON output.
|
void |
setObjectMapper(com.fr.third.fasterxml.jackson.databind.ObjectMapper objectMapper)
Set the
ObjectMapper for this view. |
void |
setPrefixJson(boolean prefixJson)
Indicate whether the JSON output by this view should be prefixed with "{} &&".
|
void |
setPrettyPrint(boolean prettyPrint)
This is a shortcut for setting up an
ObjectMapper as follows:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper); |
protected boolean |
supports(java.lang.Class<?> clazz) |
protected void |
writeInternal(java.lang.Object object,
com.fr.third.springframework.http.HttpOutputMessage outputMessage) |
canRead, canWrite, getContentLength, getDefaultContentType, getSupportedMediaTypes, read, setSupportedMediaTypes, write
public MappingJackson2HttpMessageConverter()
MappingJackson2HttpMessageConverter
.public void setObjectMapper(com.fr.third.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper
for this view.
If not set, a default ObjectMapper
is used.
Setting a custom-configured ObjectMapper
is one way to take further
control of the JSON serialization process. For example, an extended
can be configured that provides custom serializers for specific types.
The other option for refining the serialization process is to use Jackson's
provided annotations on the types to be serialized, in which case a
custom-configured ObjectMapper is unnecessary.
public com.fr.third.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
ObjectMapper
for this view.public void setJsonPrefix(java.lang.String jsonPrefix)
public void setPrefixJson(boolean prefixJson)
Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the string, the prefix would need to be ignored.
public void setPrettyPrint(boolean prettyPrint)
ObjectMapper
as follows:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper);
public boolean canRead(java.lang.Class<?> clazz, com.fr.third.springframework.http.MediaType mediaType)
canRead
在接口中 com.fr.third.springframework.http.converter.HttpMessageConverter<java.lang.Object>
canRead
在类中 com.fr.third.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
public boolean canRead(java.lang.reflect.Type type, java.lang.Class<?> contextClass, com.fr.third.springframework.http.MediaType mediaType)
canRead
在接口中 com.fr.third.springframework.http.converter.GenericHttpMessageConverter<java.lang.Object>
public boolean canWrite(java.lang.Class<?> clazz, com.fr.third.springframework.http.MediaType mediaType)
canWrite
在接口中 com.fr.third.springframework.http.converter.HttpMessageConverter<java.lang.Object>
canWrite
在类中 com.fr.third.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
protected boolean supports(java.lang.Class<?> clazz)
supports
在类中 com.fr.third.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
protected java.lang.Object readInternal(java.lang.Class<?> clazz, com.fr.third.springframework.http.HttpInputMessage inputMessage) throws java.io.IOException, com.fr.third.springframework.http.converter.HttpMessageNotReadableException
readInternal
在类中 com.fr.third.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
java.io.IOException
com.fr.third.springframework.http.converter.HttpMessageNotReadableException
public java.lang.Object read(java.lang.reflect.Type type, java.lang.Class<?> contextClass, com.fr.third.springframework.http.HttpInputMessage inputMessage) throws java.io.IOException, com.fr.third.springframework.http.converter.HttpMessageNotReadableException
read
在接口中 com.fr.third.springframework.http.converter.GenericHttpMessageConverter<java.lang.Object>
java.io.IOException
com.fr.third.springframework.http.converter.HttpMessageNotReadableException
protected void writeInternal(java.lang.Object object, com.fr.third.springframework.http.HttpOutputMessage outputMessage) throws java.io.IOException, com.fr.third.springframework.http.converter.HttpMessageNotWritableException
writeInternal
在类中 com.fr.third.springframework.http.converter.AbstractHttpMessageConverter<java.lang.Object>
java.io.IOException
com.fr.third.springframework.http.converter.HttpMessageNotWritableException
protected com.fr.third.fasterxml.jackson.databind.JavaType getJavaType(java.lang.reflect.Type type, java.lang.Class<?> contextClass)
JavaType
for the specified type and context class.
The default implementation returns typeFactory.constructType(type, contextClass)
,
but this can be overridden in subclasses, to allow for custom generic collection handling.
For instance:
protected JavaType getJavaType(Type type) { if (type instanceof Class && List.class.isAssignableFrom((Class)type)) { return TypeFactory.collectionType(ArrayList.class, MyBean.class); } else { return super.getJavaType(type); } }
type
- the type to return the java type forcontextClass
- a context class for the target type, for example a class
in which the target type appears in a method signature, can be null
signature, can be null
protected com.fr.third.fasterxml.jackson.core.JsonEncoding getJsonEncoding(com.fr.third.springframework.http.MediaType contentType)
contentType
- the media type as requested by the callernull
)