Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
0cb6f8c8
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 搜索 >>
提交
0cb6f8c8
编写于
6月 09, 2014
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish MappingJackson2JsonView
上级
5dc27ee1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
36 addition
and
37 deletion
+36
-37
spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java
...mework/web/servlet/view/json/MappingJackson2JsonView.java
+36
-37
未找到文件。
spring-webmvc/src/main/java/org/springframework/web/servlet/view/json/MappingJackson2JsonView.java
浏览文件 @
0cb6f8c8
...
...
@@ -32,6 +32,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import
org.springframework.http.converter.json.MappingJacksonValue
;
import
org.springframework.util.Assert
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.servlet.View
;
import
org.springframework.web.servlet.view.AbstractView
;
...
...
@@ -61,8 +62,6 @@ public class MappingJackson2JsonView extends AbstractView {
public
static
final
String
DEFAULT_JSONP_CONTENT_TYPE
=
"application/javascript"
;
public
static
final
String
[]
DEFAULT_JSONP_PARAMETER_NAMES
=
{
"jsonp"
,
"callback"
};
private
ObjectMapper
objectMapper
=
new
ObjectMapper
();
...
...
@@ -72,6 +71,8 @@ public class MappingJackson2JsonView extends AbstractView {
private
Boolean
prettyPrint
;
private
final
List
<
String
>
jsonpParameterNames
=
new
ArrayList
<
String
>(
Arrays
.
asList
(
"jsonp"
,
"callback"
));
private
Set
<
String
>
modelKeys
;
private
boolean
extractValueFromSingleKeyModel
=
false
;
...
...
@@ -80,8 +81,6 @@ public class MappingJackson2JsonView extends AbstractView {
private
boolean
updateContentLength
=
false
;
private
String
[]
jsonpParameterNames
;
/**
* Construct a new {@code MappingJackson2JsonView}, setting the content type to {@code application/json}.
...
...
@@ -89,7 +88,6 @@ public class MappingJackson2JsonView extends AbstractView {
public
MappingJackson2JsonView
()
{
setContentType
(
DEFAULT_CONTENT_TYPE
);
setExposePathVariables
(
false
);
this
.
jsonpParameterNames
=
DEFAULT_JSONP_PARAMETER_NAMES
;
}
...
...
@@ -171,6 +169,23 @@ public class MappingJackson2JsonView extends AbstractView {
}
}
/**
* Set JSONP request parameter names. Each time a request has one of those
* parameters, the resulting JSON will be wrapped into a function named as
* specified by the JSONP request parameter value.
*
* <p>The parameter names configured by default are "jsonp" and "callback".
*
* @since 4.1
* @see <a href="http://en.wikipedia.org/wiki/JSONP">JSONP Wikipedia article</a>
*/
public
void
setJsonpParameterNames
(
Collection
<
String
>
jsonpParameters
)
{
this
.
jsonpParameterNames
.
clear
();
if
(
jsonpParameters
!=
null
)
{
this
.
jsonpParameterNames
.
addAll
(
jsonpParameters
);
}
}
/**
* Set the attribute in the model that should be rendered by this view.
* When set, all other model attributes will be ignored.
...
...
@@ -242,21 +257,6 @@ public class MappingJackson2JsonView extends AbstractView {
this
.
updateContentLength
=
updateContentLength
;
}
/**
* Set the names of the request parameters recognized as JSONP ones.
* Each time a request has one of those parameters, the resulting JSON will
* be wrapped into a function named as specified by the JSONP parameter value.
*
* Default JSONP parameter names are "jsonp" and "callback".
*
* @since 4.1
* @see <a href="http://en.wikipedia.org/wiki/JSONP">JSONP Wikipedia article</a>
*/
public
void
setJsonpParameterNames
(
Collection
<
String
>
jsonpParameterNames
)
{
Assert
.
isTrue
(!
CollectionUtils
.
isEmpty
(
jsonpParameterNames
),
"At least one JSONP query parameter name is required"
);
this
.
jsonpParameterNames
=
jsonpParameterNames
.
toArray
(
new
String
[
jsonpParameterNames
.
size
()]);
}
@Override
protected
void
prepareResponse
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
setResponseContentType
(
request
,
response
);
...
...
@@ -268,23 +268,13 @@ public class MappingJackson2JsonView extends AbstractView {
}
}
@Override
protected
void
setResponseContentType
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
if
(
getJsonpParameterValue
(
request
)
!=
null
)
{
response
.
setContentType
(
DEFAULT_JSONP_CONTENT_TYPE
);
}
else
{
super
.
setResponseContentType
(
request
,
response
);
}
}
@Override
protected
void
renderMergedOutputModel
(
Map
<
String
,
Object
>
model
,
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
Exception
{
OutputStream
stream
=
(
this
.
updateContentLength
?
createTemporaryOutputStream
()
:
response
.
getOutputStream
());
Class
<?>
serializationView
=
(
Class
<?>)
model
.
get
(
JsonView
.
class
.
getName
());
Class
<?>
serializationView
=
(
Class
<?>)
model
.
get
(
JsonView
.
class
.
getName
());
String
jsonpParameterValue
=
getJsonpParameterValue
(
request
);
Object
value
=
filterModel
(
model
);
if
(
serializationView
!=
null
||
jsonpParameterValue
!=
null
)
{
...
...
@@ -301,14 +291,13 @@ public class MappingJackson2JsonView extends AbstractView {
}
private
String
getJsonpParameterValue
(
HttpServletRequest
request
)
{
String
jsonpParameterValue
=
null
;
for
(
String
jsonpParameterName
:
this
.
jsonpParameterNames
)
{
jsonpParameterValue
=
request
.
getParameter
(
jsonpParameterName
);
if
(
jsonpParameterValue
!=
null
)
{
break
;
for
(
String
name
:
this
.
jsonpParameterNames
)
{
String
value
=
request
.
getParameter
(
name
);
if
(!
StringUtils
.
isEmpty
(
value
))
{
return
value
;
}
}
return
jsonpParameterValue
;
return
null
;
}
/**
...
...
@@ -380,4 +369,14 @@ public class MappingJackson2JsonView extends AbstractView {
}
}
@Override
protected
void
setResponseContentType
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
if
(
getJsonpParameterValue
(
request
)
!=
null
)
{
response
.
setContentType
(
DEFAULT_JSONP_CONTENT_TYPE
);
}
else
{
super
.
setResponseContentType
(
request
,
response
);
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录