类 AbstractHttpMessageConverter<T>
- java.lang.Object
- org.springframework.http.converter.AbstractHttpMessageConverter<T>
- 类型参数:
T
- the converted object type
- 所有已实现的接口:
HttpMessageConverter<T>
- 直接已知子类:
AbstractGenericHttpMessageConverter
,AbstractWireFeedHttpMessageConverter
,AbstractXmlHttpMessageConverter
,ByteArrayHttpMessageConverter
,ObjectToStringHttpMessageConverter
,ProtobufHttpMessageConverter
,ResourceHttpMessageConverter
,SourceHttpMessageConverter
,StringHttpMessageConverter
public abstract class AbstractHttpMessageConverter<T> extends Object implements HttpMessageConverter<T>
Abstract base class for mostHttpMessageConverter
implementations.This base class adds support for setting supported
MediaTypes
, through thesupportedMediaTypes
bean property. It also adds support forContent-Type
andContent-Length
when writing to output messages.- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Juergen Hoeller, Sebastien Deleuze
构造器概要
构造器 限定符 构造器 说明 protected
AbstractHttpMessageConverter()
Construct anAbstractHttpMessageConverter
with no supported media types.protected
AbstractHttpMessageConverter(Charset defaultCharset, MediaType... supportedMediaTypes)
Construct anAbstractHttpMessageConverter
with a default charset and multiple supported media types.protected
AbstractHttpMessageConverter(MediaType supportedMediaType)
Construct anAbstractHttpMessageConverter
with one supported media type.protected
AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
Construct anAbstractHttpMessageConverter
with multiple supported media types.
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 protected void
addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType)
Add default headers to the output message.boolean
canRead(Class<?> clazz, MediaType mediaType)
This implementation checks if the given class is supported, and if the supported media typesinclude the given media type.protected boolean
canRead(MediaType mediaType)
boolean
canWrite(Class<?> clazz, MediaType mediaType)
protected boolean
canWrite(MediaType mediaType)
Returnstrue
if the given media type includes any of the supported media types.protected Long
getContentLength(T t, MediaType contentType)
Returns the content length for the given type.Charset
getDefaultCharset()
Return the default character set, if any.protected MediaType
getDefaultContentType(T t)
Returns the default content type for the given type.List<MediaType>
getSupportedMediaTypes()
Return the list ofMediaType
objects supported by this converter.T
read(Class<? extends T> clazz, HttpInputMessage inputMessage)
This implementation simple delegates toreadInternal(Class, HttpInputMessage)
.protected abstract T
readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)
Abstract template method that reads the actual object.void
setDefaultCharset(Charset defaultCharset)
Set the default character set, if any.void
setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
Set the list ofMediaType
objects supported by this converter.protected abstract boolean
supports(Class<?> clazz)
Indicates whether the given class is supported by this converter.void
write(T t, MediaType contentType, HttpOutputMessage outputMessage)
This implementation sets the default headers by callingaddDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType)
, and then callswriteInternal(T, org.springframework.http.HttpOutputMessage)
.protected abstract void
writeInternal(T t, HttpOutputMessage outputMessage)
Abstract template method that writes the actual body.
构造器详细资料
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter()
Construct anAbstractHttpMessageConverter
with no supported media types.
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter(MediaType supportedMediaType)
Construct anAbstractHttpMessageConverter
with one supported media type.- 参数:
supportedMediaType
- the supported media type
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
Construct anAbstractHttpMessageConverter
with multiple supported media types.- 参数:
supportedMediaTypes
- the supported media types
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter(Charset defaultCharset, MediaType... supportedMediaTypes)
Construct anAbstractHttpMessageConverter
with a default charset and multiple supported media types.- 参数:
defaultCharset
- the default character setsupportedMediaTypes
- the supported media types- 从以下版本开始:
- 4.3
方法详细资料
setSupportedMediaTypes
public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
Set the list ofMediaType
objects supported by this converter.
getSupportedMediaTypes
public List<MediaType> getSupportedMediaTypes()
从接口复制的说明:HttpMessageConverter
Return the list ofMediaType
objects supported by this converter.- 指定者:
getSupportedMediaTypes
在接口中HttpMessageConverter<T>
- 返回:
- the list of supported media types, potentially an immutable copy
setDefaultCharset
public void setDefaultCharset(@Nullable Charset defaultCharset)
Set the default character set, if any.- 从以下版本开始:
- 4.3
getDefaultCharset
@Nullable public Charset getDefaultCharset()
Return the default character set, if any.- 从以下版本开始:
- 4.3
canRead
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType)
This implementation checks if the given class is supported, and if the supported media typesinclude the given media type.- 指定者:
canRead
在接口中HttpMessageConverter<T>
- 参数:
clazz
- the class to test for readabilitymediaType
- the media type to read (can benull
if not specified); typically the value of aContent-Type
header.- 返回:
true
if readable;false
otherwise
canRead
protected boolean canRead(@Nullable MediaType mediaType)
- 参数:
mediaType
- the media type to read, can benull
if not specified. Typically the value of aContent-Type
header.- 返回:
true
if the supported media types include the media type, or if the media type isnull
canWrite
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType)
This implementation checks if the given class is supported, and if the supported media types include the given media type.- 指定者:
canWrite
在接口中HttpMessageConverter<T>
- 参数:
clazz
- the class to test for writabilitymediaType
- the media type to write (can benull
if not specified); typically the value of anAccept
header.- 返回:
true
if writable;false
otherwise
canWrite
protected boolean canWrite(@Nullable MediaType mediaType)
Returnstrue
if the given media type includes any of the supported media types.- 参数:
mediaType
- the media type to write, can benull
if not specified. Typically the value of anAccept
header.- 返回:
true
if the supported media types are compatible with the media type, or if the media type isnull
read
public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
This implementation simple delegates toreadInternal(Class, HttpInputMessage)
. Future implementations might add some default behavior, however.- 指定者:
read
在接口中HttpMessageConverter<T>
- 参数:
clazz
- the type of object to return. This type must have previously been passed to thecanRead
method of this interface, which must have returnedtrue
.inputMessage
- the HTTP input message to read from- 返回:
- the converted object
- 抛出:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
write
public final void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
This implementation sets the default headers by callingaddDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType)
, and then callswriteInternal(T, org.springframework.http.HttpOutputMessage)
.- 指定者:
write
在接口中HttpMessageConverter<T>
- 参数:
t
- the object to write to the output message. The type of this object must have previously been passed to thecanWrite
method of this interface, which must have returnedtrue
.contentType
- the content type to use when writing. May benull
to indicate that the default content type of the converter must be used. If notnull
, this media type must have previously been passed to thecanWrite
method of this interface, which must have returnedtrue
.outputMessage
- the message to write to- 抛出:
IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors
addDefaultHeaders
protected void addDefaultHeaders(HttpHeaders headers, T t, @Nullable MediaType contentType) throws IOException
Add default headers to the output message.This implementation delegates to
getDefaultContentType(Object)
if a content type was not provided, set if necessary the default character set, callsgetContentLength(T, org.springframework.http.MediaType)
, and sets the corresponding headers.- 抛出:
IOException
- 从以下版本开始:
- 4.2
getDefaultContentType
@Nullable protected MediaType getDefaultContentType(T t) throws IOException
Returns the default content type for the given type. Called whenwrite(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
is invoked without a specified content type parameter.By default, this returns the first element of the
supportedMediaTypes
property, if any. Can be overridden in subclasses.- 参数:
t
- the type to return the content type for- 返回:
- the content type, or
null
if not known - 抛出:
IOException
getContentLength
@Nullable protected Long getContentLength(T t, @Nullable MediaType contentType) throws IOException
Returns the content length for the given type.By default, this returns
null
, meaning that the content length is unknown. Can be overridden in subclasses.- 参数:
t
- the type to return the content length for- 返回:
- the content length, or
null
if not known - 抛出:
IOException
supports
protected abstract boolean supports(Class<?> clazz)
Indicates whether the given class is supported by this converter.- 参数:
clazz
- the class to test for support- 返回:
true
if supported;false
otherwise
readInternal
protected abstract T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
Abstract template method that reads the actual object. Invoked fromread(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.- 参数:
clazz
- the type of object to returninputMessage
- the HTTP input message to read from- 返回:
- the converted object
- 抛出:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
writeInternal
protected abstract void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
Abstract template method that writes the actual body. Invoked fromwrite(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.- 参数:
t
- the object to write to the output messageoutputMessage
- the HTTP output message to write to- 抛出:
IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors