Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
59e41a27
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,发现更多精彩内容 >>
提交
59e41a27
编写于
6月 11, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-5808 - Make HttpMessageConverterExtractor top level class
上级
58d3e704
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
73 addition
and
33 deletion
+73
-33
org.springframework.web/src/main/java/org/springframework/web/client/HttpMessageConverterExtractor.java
...ngframework/web/client/HttpMessageConverterExtractor.java
+69
-0
org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java
...ain/java/org/springframework/web/client/RestTemplate.java
+4
-33
未找到文件。
org.springframework.web/src/main/java/org/springframework/web/client/HttpMessageConverterExtractor.java
0 → 100644
浏览文件 @
59e41a27
/*
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.web.client
;
import
java.io.IOException
;
import
java.util.List
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.client.ClientHttpResponse
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.util.Assert
;
/**
* Response extractor that uses the given {@linkplain HttpMessageConverter entity converters} to convert the response
* into a type <code>T</code>.
*
* @author Arjen Poutsma
* @see RestTemplate
* @since 3.0
*/
public
class
HttpMessageConverterExtractor
<
T
>
implements
ResponseExtractor
<
T
>
{
private
final
Class
<
T
>
responseType
;
private
final
List
<
HttpMessageConverter
<
T
>>
messageConverters
;
/**
* Creates a new instance of the {@code HttpMessageConverterExtractor} with the given response type and message
* converters. The given converters must support the response type.
*/
public
HttpMessageConverterExtractor
(
Class
<
T
>
responseType
,
List
<
HttpMessageConverter
<
T
>>
messageConverters
)
{
Assert
.
notNull
(
responseType
,
"'responseType' must not be null"
);
Assert
.
notEmpty
(
messageConverters
,
"'messageConverters' must not be empty"
);
this
.
responseType
=
responseType
;
this
.
messageConverters
=
messageConverters
;
}
public
T
extractData
(
ClientHttpResponse
response
)
throws
IOException
{
MediaType
contentType
=
response
.
getHeaders
().
getContentType
();
if
(
contentType
==
null
)
{
throw
new
RestClientException
(
"Cannot extract response: no Content-Type found"
);
}
for
(
HttpMessageConverter
<
T
>
messageConverter
:
messageConverters
)
{
for
(
MediaType
supportedMediaType
:
messageConverter
.
getSupportedMediaTypes
())
{
if
(
supportedMediaType
.
includes
(
contentType
))
{
return
messageConverter
.
read
(
this
.
responseType
,
response
);
}
}
}
throw
new
RestClientException
(
"Could not extract response: no suitable HttpMessageConverter found for response type ["
+
this
.
responseType
.
getName
()
+
"] and content type ["
+
contentType
+
"]"
);
}
}
org.springframework.web/src/main/java/org/springframework/web/client/RestTemplate.java
浏览文件 @
59e41a27
...
...
@@ -166,16 +166,18 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
public
<
T
>
T
getForObject
(
String
url
,
Class
<
T
>
responseType
,
String
...
urlVariables
)
throws
RestClientException
{
checkForSupportedMessageConverter
(
responseType
);
List
<
HttpMessageConverter
<
T
>>
supportedMessageConverters
=
getSupportedMessageConverters
(
responseType
);
return
execute
(
url
,
HttpMethod
.
GET
,
new
GetCallback
<
T
>(
responseType
),
new
HttpMessageConverterExtractor
<
T
>(
responseType
),
urlVariables
);
new
HttpMessageConverterExtractor
<
T
>(
responseType
,
supportedMessageConverters
),
urlVariables
);
}
public
<
T
>
T
getForObject
(
String
url
,
Class
<
T
>
responseType
,
Map
<
String
,
String
>
urlVariables
)
throws
RestClientException
{
checkForSupportedMessageConverter
(
responseType
);
List
<
HttpMessageConverter
<
T
>>
supportedMessageConverters
=
getSupportedMessageConverters
(
responseType
);
return
execute
(
url
,
HttpMethod
.
GET
,
new
GetCallback
<
T
>(
responseType
),
new
HttpMessageConverterExtractor
<
T
>(
responseType
),
urlVariables
);
new
HttpMessageConverterExtractor
<
T
>(
responseType
,
supportedMessageConverters
),
urlVariables
);
}
// HEAD
...
...
@@ -381,37 +383,6 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
}
}
/**
* Response extractor that uses the registered {@linkplain HttpMessageConverter entity converters}
* to convert the response into a type <code>T</code>.
*/
private
class
HttpMessageConverterExtractor
<
T
>
implements
ResponseExtractor
<
T
>
{
private
final
Class
<
T
>
responseType
;
private
HttpMessageConverterExtractor
(
Class
<
T
>
responseType
)
{
this
.
responseType
=
responseType
;
}
public
T
extractData
(
ClientHttpResponse
response
)
throws
IOException
{
MediaType
contentType
=
response
.
getHeaders
().
getContentType
();
if
(
contentType
==
null
)
{
throw
new
RestClientException
(
"Cannot extract response: no Content-Type found"
);
}
for
(
HttpMessageConverter
<
T
>
messageConverter
:
getSupportedMessageConverters
(
this
.
responseType
))
{
for
(
MediaType
supportedMediaType
:
messageConverter
.
getSupportedMediaTypes
())
{
if
(
supportedMediaType
.
includes
(
contentType
))
{
return
messageConverter
.
read
(
this
.
responseType
,
response
);
}
}
}
throw
new
RestClientException
(
"Could not extract response: no suitable HttpMessageConverter found for response type ["
+
this
.
responseType
.
getName
()
+
"] and content type ["
+
contentType
+
"]"
);
}
}
/** Response extractor that extracts the response {@link HttpHeaders}. */
private
static
class
HeadersExtractor
implements
ResponseExtractor
<
HttpHeaders
>
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录