Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
后端镜像
java镜像
Springside4
提交
7eb7d99f
S
Springside4
项目概览
后端镜像
/
java镜像
/
Springside4
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Springside4
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7eb7d99f
编写于
4月 07, 2012
作者:
C
Calvin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#21 cleanup 基础类
上级
bd27910d
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
47 addition
and
44 deletion
+47
-44
examples/showcase/src/main/java/org/springside/examples/showcase/utilities/json/JsonDemo.java
...springside/examples/showcase/utilities/json/JsonDemo.java
+4
-4
examples/showcase/src/main/java/org/springside/examples/showcase/web/MashupServerController.java
...ingside/examples/showcase/web/MashupServerController.java
+1
-1
modules/core/src/main/java/org/springside/modules/mapper/JsonMapper.java
...c/main/java/org/springside/modules/mapper/JsonMapper.java
+14
-18
modules/core/src/main/java/org/springside/modules/test/functional/WebDriverFactory.java
.../springside/modules/test/functional/WebDriverFactory.java
+2
-0
modules/core/src/main/java/org/springside/modules/utils/Collections3.java
.../main/java/org/springside/modules/utils/Collections3.java
+5
-2
modules/core/src/main/java/org/springside/modules/utils/PropertiesLoader.java
...n/java/org/springside/modules/utils/PropertiesLoader.java
+12
-2
modules/core/src/test/java/org/springside/modules/mapper/JsonMapperTest.java
...st/java/org/springside/modules/mapper/JsonMapperTest.java
+1
-1
pom.xml
pom.xml
+8
-16
未找到文件。
examples/showcase/src/main/java/org/springside/examples/showcase/utilities/json/JsonDemo.java
浏览文件 @
7eb7d99f
...
...
@@ -133,7 +133,7 @@ public class JsonDemo {
TestBean
bean
=
new
TestBean
(
"A"
);
//打印全部属性
JsonMapper
normalMapper
=
JsonMapper
.
buildNormal
Mapper
();
JsonMapper
normalMapper
=
new
Json
Mapper
();
assertEquals
(
"{\"name\":\"A\",\"defaultValue\":\"hello\",\"nullValue\":null}"
,
normalMapper
.
toJson
(
bean
));
//不打印nullValue属性
...
...
@@ -198,7 +198,7 @@ public class JsonDemo {
TestBean
bean
=
new
TestBean
();
bean
.
setDefaultValue
(
"Foobar"
);
bean
=
mapper
.
update
(
bean
,
jsonString
);
bean
=
mapper
.
update
(
jsonString
,
bean
);
//name被赋值
assertEquals
(
"A"
,
bean
.
getName
());
//DefaultValue不在Json串中,依然保留。
...
...
@@ -263,9 +263,9 @@ public class JsonDemo {
assertEquals
(
TestEnum
.
One
,
mapper
.
fromJson
(
"\"One\""
,
TestEnum
.
class
));
//使用enum.toString()
//注意,index會通過toString序列成字符串而不是int,否則
又和
順序號混淆.
//注意,index會通過toString序列成字符串而不是int,否則
会和order()
順序號混淆.
//注意配置必須在所有讀寫動作之前調用.
JsonMapper
newMapper
=
JsonMapper
.
buildNormal
Mapper
();
JsonMapper
newMapper
=
new
Json
Mapper
();
newMapper
.
enableEnumUseToString
();
assertEquals
(
"\"1\""
,
newMapper
.
toJson
(
TestEnum
.
One
));
assertEquals
(
TestEnum
.
One
,
newMapper
.
fromJson
(
"\"1\""
,
TestEnum
.
class
));
...
...
examples/showcase/src/main/java/org/springside/examples/showcase/web/MashupServerController.java
浏览文件 @
7eb7d99f
...
...
@@ -19,7 +19,7 @@ public class MashupServerController {
private
static
final
String
DEFAULT_JQUERY_JSONP_CALLBACK_PARM_NAME
=
"callback"
;
private
JsonMapper
mapper
=
JsonMapper
.
buildNormal
Mapper
();
private
JsonMapper
mapper
=
new
Json
Mapper
();
@RequestMapping
(
"/web/mashup"
)
@ResponseBody
...
...
modules/core/src/main/java/org/springside/modules/mapper/JsonMapper.java
浏览文件 @
7eb7d99f
...
...
@@ -32,21 +32,16 @@ public class JsonMapper {
private
ObjectMapper
mapper
;
public
JsonMapper
()
{
this
(
Include
.
ALWAYS
);
}
public
JsonMapper
(
Include
include
)
{
mapper
=
new
ObjectMapper
();
//设置输出时包含属性的风格
mapper
.
setSerializationInclusion
(
include
);
//设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
mapper
.
disable
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
);
//禁止使用int代表Enum的order()來反序列化Enum,非常危險
mapper
.
enable
(
DeserializationFeature
.
FAIL_ON_NUMBERS_FOR_ENUMS
);
}
/**
* 创建输出全部属性到Json字符串的Mapper.
*/
public
static
JsonMapper
buildNormalMapper
()
{
return
new
JsonMapper
(
Include
.
ALWAYS
);
}
/**
...
...
@@ -71,6 +66,7 @@ public class JsonMapper {
}
/**
* Object可以是POJO,也可以是Collection或数组。
* 如果对象为Null, 返回"null".
* 如果集合为空集合, 返回"[]".
*/
...
...
@@ -85,11 +81,13 @@ public class JsonMapper {
}
/**
* 反序列化POJO或简单Collection如List<String>.
*
* 如果JSON字符串为Null或"null"字符串, 返回Null.
* 如果JSON字符串为"[]", 返回空集合.
*
* 如需
读取集合如List/Map, 且不是List<String>这种简单类型时,先使用函數constructParametricType构造类型.
* @see #
createCollectionType(Class, Class...
)
* 如需
反序列化复杂Collection如List<MyBean>, 请使用fromJson(String,JavaType)
* @see #
fromJson(String, JavaType
)
*/
public
<
T
>
T
fromJson
(
String
jsonString
,
Class
<
T
>
clazz
)
{
if
(
StringUtils
.
isEmpty
(
jsonString
))
{
...
...
@@ -105,10 +103,7 @@ public class JsonMapper {
}
/**
* 如果JSON字符串为Null或"null"字符串, 返回Null.
* 如果JSON字符串为"[]", 返回空集合.
*
* 如需读取集合如List/Map, 且不是List<String>这种简单类型时,先使用函數constructParametricType构造类型.
* 反序列化复杂Collection如List<Bean>, 先使用函數createCollectionType构造类型,然后调用本函数.
* @see #createCollectionType(Class, Class...)
*/
public
<
T
>
T
fromJson
(
String
jsonString
,
JavaType
javaType
)
{
...
...
@@ -125,8 +120,9 @@ public class JsonMapper {
}
/**
* 構造泛型的Type如List<MyBean>, 则调用constructCollectionType(ArrayList.class,MyBean.class)
* Map<String,MyBean>则调用(HashMap.class,String.class, MyBean.class)
* 構造泛型的Collection Type如:
* ArrayList<MyBean>, 则调用constructCollectionType(ArrayList.class,MyBean.class)
* HashMap<String,MyBean>, 则调用(HashMap.class,String.class, MyBean.class)
*/
public
JavaType
createCollectionType
(
Class
<?>
collectionClass
,
Class
<?>...
elementClasses
)
{
return
mapper
.
getTypeFactory
().
constructParametricType
(
collectionClass
,
elementClasses
);
...
...
@@ -135,7 +131,7 @@ public class JsonMapper {
/**
* 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
*/
public
<
T
>
T
update
(
T
object
,
String
jsonString
)
{
public
<
T
>
T
update
(
String
jsonString
,
T
object
)
{
try
{
return
(
T
)
mapper
.
readerForUpdating
(
object
).
readValue
(
jsonString
);
}
catch
(
JsonProcessingException
e
)
{
...
...
modules/core/src/main/java/org/springside/modules/test/functional/WebDriverFactory.java
浏览文件 @
7eb7d99f
...
...
@@ -61,6 +61,8 @@ public class WebDriverFactory {
cap
=
DesiredCapabilities
.
firefox
();
}
else
if
(
BrowserType
.
ie
.
name
().
equals
(
driverType
))
{
cap
=
DesiredCapabilities
.
internetExplorer
();
}
else
if
(
BrowserType
.
chrome
.
name
().
equals
(
driverType
))
{
cap
=
DesiredCapabilities
.
chrome
();
}
try
{
...
...
modules/core/src/main/java/org/springside/modules/utils/Collections3.java
浏览文件 @
7eb7d99f
...
...
@@ -26,6 +26,9 @@ public class Collections3 {
private
Collections3
()
{
}
/**
* 返回a-b的集合.
*/
public
static
<
T
>
List
<
T
>
subtract
(
final
Collection
<
T
>
a
,
final
Collection
<
T
>
b
)
{
ArrayList
<
T
>
list
=
new
ArrayList
<
T
>(
a
);
for
(
Iterator
it
=
b
.
iterator
();
it
.
hasNext
();)
{
...
...
@@ -43,7 +46,7 @@ public class Collections3 {
*/
public
static
Map
extractToMap
(
final
Collection
collection
,
final
String
keyPropertyName
,
final
String
valuePropertyName
)
{
Map
map
=
new
HashMap
();
Map
map
=
new
HashMap
(
collection
.
size
()
);
try
{
for
(
Object
obj
:
collection
)
{
...
...
@@ -64,7 +67,7 @@ public class Collections3 {
* @param propertyName 要提取的属性名.
*/
public
static
List
extractToList
(
final
Collection
collection
,
final
String
propertyName
)
{
List
list
=
new
ArrayList
();
List
list
=
new
ArrayList
(
collection
.
size
()
);
try
{
for
(
Object
obj
:
collection
)
{
...
...
modules/core/src/main/java/org/springside/modules/utils/PropertiesLoader.java
浏览文件 @
7eb7d99f
...
...
@@ -34,7 +34,7 @@ public class PropertiesLoader {
private
Properties
properties
;
public
PropertiesLoader
(
String
...
resourcesPaths
)
throws
IOException
{
public
PropertiesLoader
(
String
...
resourcesPaths
)
{
properties
=
loadProperties
(
resourcesPaths
);
}
...
...
@@ -53,10 +53,20 @@ public class PropertiesLoader {
return
properties
.
getProperty
(
key
);
}
public
String
getProperty
(
String
key
,
String
defaultValue
)
{
String
result
=
getProperty
(
key
);
if
(
result
!=
null
)
{
return
result
;
}
else
{
return
defaultValue
;
}
}
/**
* 载入多个文件, 文件路径使用Spring Resource格式.
*/
private
Properties
loadProperties
(
String
...
resourcesPaths
)
throws
IOException
{
private
Properties
loadProperties
(
String
...
resourcesPaths
)
{
Properties
props
=
new
Properties
();
for
(
String
location
:
resourcesPaths
)
{
...
...
modules/core/src/test/java/org/springside/modules/mapper/JsonMapperTest.java
浏览文件 @
7eb7d99f
...
...
@@ -140,7 +140,7 @@ public class JsonMapperTest {
@Test
public
void
threeTypeBinders
()
{
//打印全部属性
JsonMapper
normalBinder
=
JsonMapper
.
buildNormal
Mapper
();
JsonMapper
normalBinder
=
new
Json
Mapper
();
TestBean
bean
=
new
TestBean
(
"A"
);
assertEquals
(
"{\"name\":\"A\",\"defaultValue\":\"hello\",\"nullValue\":null}"
,
normalBinder
.
toJson
(
bean
));
...
...
pom.xml
浏览文件 @
7eb7d99f
...
...
@@ -17,10 +17,13 @@
<jersey.version>
1.12
</jersey.version>
<shiro.version>
1.2.0
</shiro.version>
<activemq.version>
5.5.1
</activemq.version>
<jetty.version>
7.6.2.v20120308
</jetty.version>
<h2.version>
1.3.164
</h2.version>
<quartz.version>
2.1.3
</quartz.version>
<ehcache.version>
2.5.1
</ehcache.version>
<hibernate-validator.version>
4.2.0.Final
</hibernate-validator.version>
<jackson.version>
2.0.0
</jackson.version>
<slf4j.version>
1.6.4
</slf4j.version>
<jetty.version>
7.6.2.v20120308
</jetty.version>
<h2.version>
1.3.164
</h2.version>
<selenium.version>
2.20.0
</selenium.version>
<!-- Plugin的属性定义 -->
...
...
@@ -135,11 +138,6 @@
<artifactId>
aspectjweaver
</artifactId>
<version>
1.6.12
</version>
</dependency>
<dependency>
<groupId>
cglib
</groupId>
<artifactId>
cglib-nodep
</artifactId>
<version>
2.2.2
</version>
</dependency>
<dependency>
<groupId>
cglib
</groupId>
<artifactId>
cglib
</artifactId>
...
...
@@ -175,12 +173,6 @@
<artifactId>
hibernate-jpa-2.0-api
</artifactId>
<version>
1.0.1.Final
</version>
</dependency>
<dependency>
<groupId>
joda-time
</groupId>
<artifactId>
joda-time-hibernate
</artifactId>
<version>
1.3
</version>
</dependency>
<!-- spring data jpa -->
<dependency>
...
...
@@ -399,7 +391,7 @@
<dependency>
<groupId>
org.quartz-scheduler
</groupId>
<artifactId>
quartz
</artifactId>
<version>
2.1.3
</version>
<version>
${quartz.version}
</version>
</dependency>
<!-- CACHE begin -->
...
...
@@ -422,7 +414,7 @@
<dependency>
<groupId>
net.sf.ehcache
</groupId>
<artifactId>
ehcache-core
</artifactId>
<version>
2.5.1
</version>
<version>
${ehcache.version}
</version>
</dependency>
<!-- CACHE end -->
...
...
@@ -618,7 +610,7 @@
<dependency>
<groupId>
org.hibernate
</groupId>
<artifactId>
hibernate-validator
</artifactId>
<version>
4.2.0.Final
</version>
<version>
${hibernate-validator.version}
</version>
</dependency>
<!-- httpclient -->
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录