Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
f92b7f10
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f92b7f10
编写于
3月 27, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added readInternal template method
上级
bcf6f232
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
55 addition
and
31 deletion
+55
-31
org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
...ramework/http/converter/AbstractHttpMessageConverter.java
+32
-13
org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java
...amework/http/converter/ByteArrayHttpMessageConverter.java
+2
-1
org.springframework.web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
...ingframework/http/converter/FormHttpMessageConverter.java
+6
-5
org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java
.../springframework/http/converter/HttpMessageConverter.java
+9
-8
org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java
...gframework/http/converter/StringHttpMessageConverter.java
+4
-3
org.springframework.web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java
...k/http/converter/xml/AbstractXmlHttpMessageConverter.java
+2
-1
未找到文件。
org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
浏览文件 @
f92b7f10
...
...
@@ -26,6 +26,7 @@ import org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpInputMessage
;
import
org.springframework.http.HttpOutputMessage
;
import
org.springframework.http.MediaType
;
import
org.springframework.util.Assert
;
...
...
@@ -34,8 +35,8 @@ import org.springframework.util.Assert;
* Abstract base class for most {@link HttpMessageConverter} implementations.
*
* <p>This base class adds support for setting supported {@code MediaTypes}, through the {@link
* #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for
* {@code Content-
Type} and {@code Content-
Length} when writing to output messages.
* #setSupportedMediaTypes(List) supportedMediaTypes} bean property. It also adds support for
{@code Content-Type} and
* {@code Content-Length} when writing to output messages.
*
* @author Arjen Poutsma
* @since 3.0
...
...
@@ -76,11 +77,29 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* <p>This implementation delegates to {@link #getContentType(Object)} and {@link #getContentLength(Object)},
* and sets the corresponding headers on the output message. It then calls
* {@link #writeInternal(Object, HttpOutputMessage)}.
* {@inheritDoc} <p>This implementation simple delegates to {@link #readInternal(Class, HttpInputMessage)}. Future
* implementations might add some default behavior, however.
*/
public
final
T
read
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
return
readInternal
(
clazz
,
inputMessage
);
}
/**
* Abstract template method that reads the actualy object. Invoked from {@link #read(Class, HttpInputMessage)}.
*
* @throws HttpMessageConversionException in case of conversion errors
* @param clazz the type of object to return
* @param inputMessage the HTTP input message to read from
* @return the converted object
* @throws IOException in case of I/O errors
* @throws HttpMessageNotReadableException in case of conversion errors
*/
protected
abstract
T
readInternal
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
,
HttpMessageNotReadableException
;
/**
* {@inheritDoc} <p>This implementation delegates to {@link #getContentType(Object)} and {@link
* #getContentLength(Object)}, and sets the corresponding headers on the output message. It then calls {@link
* #writeInternal(Object, HttpOutputMessage)}.
*/
public
final
void
write
(
T
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
{
HttpHeaders
headers
=
outputMessage
.
getHeaders
();
...
...
@@ -97,9 +116,8 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* Returns the content type for the given type.
* <p>By default, this returns the first element of the {@link #setSupportedMediaTypes(List) supportedMediaTypes}
* property, if any. Can be overriden in subclasses.
* Returns the content type for the given type. <p>By default, this returns the first element of the {@link
* #setSupportedMediaTypes(List) supportedMediaTypes} property, if any. Can be overriden in subclasses.
*
* @param t the type to return the content type for
* @return the content type, or <code>null</code> if not known
...
...
@@ -110,8 +128,8 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
}
/**
* Returns the content length for the given type.
*
<p>By default, this returns <code>null</code>. Can be overriden in
subclasses.
* Returns the content length for the given type.
<p>By default, this returns <code>null</code>. Can be overriden in
* subclasses.
*
* @param t the type to return the content length for
* @return the content length, or <code>null</code> if not known
...
...
@@ -126,8 +144,9 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
* @param t the object to write to the output message
* @param outputMessage the message to write to
* @throws IOException in case of I/O errors
* @throws HttpMessage
Conversion
Exception in case of conversion errors
* @throws HttpMessage
NotWritable
Exception in case of conversion errors
*/
protected
abstract
void
writeInternal
(
T
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
;
protected
abstract
void
writeInternal
(
T
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
,
HttpMessageNotWritableException
;
}
org.springframework.web/src/main/java/org/springframework/http/converter/ByteArrayHttpMessageConverter.java
浏览文件 @
f92b7f10
...
...
@@ -46,7 +46,8 @@ public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<
return
byte
[].
class
.
equals
(
clazz
);
}
public
byte
[]
read
(
Class
<
byte
[]>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
@Override
public
byte
[]
readInternal
(
Class
<
byte
[]>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
long
contentLength
=
inputMessage
.
getHeaders
().
getContentLength
();
if
(
contentLength
>=
0
)
{
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
((
int
)
contentLength
);
...
...
org.springframework.web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
浏览文件 @
f92b7f10
...
...
@@ -37,9 +37,9 @@ import org.springframework.util.StringUtils;
/**
* Implementation of {@link HttpMessageConverter} that can read and write form data.
*
* <p>By default, this converter reads and writes the media type ({@code application/x-www-form-urlencoded}). This
*
can be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. Form
*
data
is read from and written into a {@link MultiValueMap MultiValueMap<String, String>}.
* <p>By default, this converter reads and writes the media type ({@code application/x-www-form-urlencoded}). This
can
*
be overridden by setting the {@link #setSupportedMediaTypes(java.util.List) supportedMediaTypes} property. Form data
* is read from and written into a {@link MultiValueMap MultiValueMap<String, String>}.
*
* @author Arjen Poutsma
* @see MultiValueMap
...
...
@@ -58,8 +58,9 @@ public class FormHttpMessageConverter extends AbstractHttpMessageConverter<Multi
return
MultiValueMap
.
class
.
isAssignableFrom
(
clazz
);
}
public
MultiValueMap
<
String
,
String
>
read
(
Class
<
MultiValueMap
<
String
,
String
>>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
@Override
public
MultiValueMap
<
String
,
String
>
readInternal
(
Class
<
MultiValueMap
<
String
,
String
>>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
MediaType
contentType
=
inputMessage
.
getHeaders
().
getContentType
();
Charset
charset
=
contentType
.
getCharSet
()
!=
null
?
contentType
.
getCharSet
()
:
DEFAULT_CHARSET
;
String
body
=
FileCopyUtils
.
copyToString
(
new
InputStreamReader
(
inputMessage
.
getBody
(),
charset
));
...
...
org.springframework.web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java
浏览文件 @
f92b7f10
...
...
@@ -33,33 +33,34 @@ public interface HttpMessageConverter<T> {
/**
* Indicate whether the given class is supported by this converter.
*
* @param clazz the class to test for support
* @return <code>true</code> if supported; <code>false</code> otherwise
*/
boolean
supports
(
Class
<?
extends
T
>
clazz
);
/**
* Return the list of {@link MediaType} objects supported by this converter.
*/
/** Return the list of {@link MediaType} objects supported by this converter. */
List
<
MediaType
>
getSupportedMediaTypes
();
/**
* Read an object of the given type form the given input message, and returns it.
*
* @param clazz the type of object to return
* @param inputMessage the HTTP input message to read from
* @return the converted object
* @throws IOException in case of I/O errors
* @throws HttpMessage
Conversion
Exception in case of conversion errors
* @throws HttpMessage
NotReadable
Exception in case of conversion errors
*/
T
read
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
;
T
read
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
,
HttpMessageNotReadableException
;
/**
* Write an given object to the given output message.
* @param t the object to write to the output message
*
* @param t the object to write to the output message
* @param outputMessage the message to write to
* @throws IOException in case of I/O errors
* @throws HttpMessage
Conversion
Exception in case of conversion errors
* @throws HttpMessage
NotWritable
Exception in case of conversion errors
*/
void
write
(
T
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
;
void
write
(
T
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
,
HttpMessageNotWritableException
;
}
org.springframework.web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java
浏览文件 @
f92b7f10
...
...
@@ -54,7 +54,8 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
return
String
.
class
.
equals
(
clazz
);
}
public
String
read
(
Class
<
String
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
@Override
public
String
readInternal
(
Class
<
String
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
MediaType
contentType
=
inputMessage
.
getHeaders
().
getContentType
();
Charset
charset
=
contentType
.
getCharSet
()
!=
null
?
contentType
.
getCharSet
()
:
DEFAULT_CHARSET
;
return
FileCopyUtils
.
copyToString
(
new
InputStreamReader
(
inputMessage
.
getBody
(),
charset
));
...
...
@@ -86,8 +87,8 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
}
/**
* Return the list of supported {@link Charset}.
*
<p>By default, returns {@link Charset#availableCharsets()}. Can be
overridden in subclasses.
* Return the list of supported {@link Charset}.
<p>By default, returns {@link Charset#availableCharsets()}. Can be
* overridden in subclasses.
*
* @return the list of accepted charsets
*/
...
...
org.springframework.web/src/main/java/org/springframework/http/converter/xml/AbstractXmlHttpMessageConverter.java
浏览文件 @
f92b7f10
...
...
@@ -55,7 +55,8 @@ public abstract class AbstractXmlHttpMessageConverter<T> extends AbstractHttpMes
}
/** Invokes {@link #readFromSource(Class, HttpHeaders, Source)}. */
public
final
T
read
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
@Override
public
final
T
readInternal
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
{
return
readFromSource
(
clazz
,
inputMessage
.
getHeaders
(),
new
StreamSource
(
inputMessage
.
getBody
()));
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录