Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
3ca8c9be
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,发现更多精彩内容 >>
提交
3ca8c9be
编写于
6月 12, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-5823 - JSON HttpMessageConverter
上级
12424007
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
300 addition
and
22 deletion
+300
-22
org.springframework.web/ivy.xml
org.springframework.web/ivy.xml
+25
-22
org.springframework.web/src/main/java/org/springframework/http/converter/json/JacksonHttpMessageConverter.java
...work/http/converter/json/JacksonHttpMessageConverter.java
+88
-0
org.springframework.web/src/main/java/org/springframework/http/converter/json/package-info.java
...org/springframework/http/converter/json/package-info.java
+23
-0
org.springframework.web/src/test/java/org/springframework/http/converter/json/JacksonHttpMessageConverterTest.java
.../http/converter/json/JacksonHttpMessageConverterTest.java
+141
-0
org.springframework.web/template.mf
org.springframework.web/template.mf
+1
-0
org.springframework.web/web.iml
org.springframework.web/web.iml
+22
-0
未找到文件。
org.springframework.web/ivy.xml
浏览文件 @
3ca8c9be
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://ivyrep.jayasoft.org/ivy-doc.xsl"?>
<ivy-module
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"http://incubator.apache.org/ivy/schemas/ivy.xsd"
version=
"1.3"
>
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"http://incubator.apache.org/ivy/schemas/ivy.xsd"
version=
"1.3"
>
<info
organisation=
"org.springframework"
module=
"${ant.project.name}"
>
<license
name=
"Apache 2.0"
url=
"http://www.apache.org/licenses/LICENSE-2.0"
/>
...
...
@@ -17,6 +17,7 @@
<conf
name=
"faces"
extends=
"runtime"
description=
"JARs neeeded to use JSF"
/>
<conf
name=
"hessian"
extends=
"runtime"
description=
"JARs needed to use Hessian"
/>
<conf
name=
"httpclient"
extends=
"runtime"
description=
"JARs needed to use HTTPClient"
/>
<conf
name=
"jackson"
extends=
"runtime"
description=
"JARs needed to use the Jackson JSON converter"
/>
<conf
name=
"jaxrpc"
extends=
"runtime"
description=
"JARs needed to use JAXRPC"
/>
<conf
name=
"jsp"
extends=
"runtime"
description=
"JARs needed to use JSP"
/>
<conf
name=
"log4j"
extends=
"runtime"
description=
"JARs needed to use Log4J"
/>
...
...
@@ -30,48 +31,50 @@
<dependencies>
<dependency
org=
"com.caucho"
name=
"com.springsource.com.caucho"
rev=
"3.2.1"
conf=
"optional, hessian, burlap->compile"
/>
conf=
"optional, hessian, burlap->compile"
/>
<dependency
org=
"javax.el"
name=
"com.springsource.javax.el"
rev=
"1.0.0"
conf=
"provided, el->compile"
/>
<dependency
org=
"javax.faces"
name=
"com.springsource.javax.faces"
rev=
"1.2.0.08"
conf=
"provided, faces->compile"
/>
conf=
"provided, faces->compile"
/>
<dependency
org=
"javax.portlet"
name=
"com.springsource.javax.portlet"
rev=
"2.0.0"
conf=
"provided->compile"
/>
<dependency
org=
"javax.servlet"
name=
"com.springsource.javax.servlet"
rev=
"2.5.0"
conf=
"provided->compile"
/>
<dependency
org=
"javax.servlet"
name=
"com.springsource.javax.servlet.jsp"
rev=
"2.1.0"
conf=
"provided, jsp->compile"
/>
conf=
"provided, jsp->compile"
/>
<dependency
org=
"javax.xml.rpc"
name=
"com.springsource.javax.xml.rpc"
rev=
"1.1.0"
conf=
"provided, jaxrpc->compile"
/>
conf=
"provided, jaxrpc->compile"
/>
<dependency
org=
"javax.xml.soap"
name=
"com.springsource.javax.xml.soap"
rev=
"1.3.0"
conf=
"provided, ws->compile"
/>
conf=
"provided, ws->compile"
/>
<dependency
org=
"javax.xml.ws"
name=
"com.springsource.javax.xml.ws"
rev=
"2.1.1"
conf=
"provided, ws->compile"
/>
<dependency
org=
"org.aopalliance"
name=
"com.springsource.org.aopalliance"
rev=
"1.0.0"
conf=
"compile->compile"
/>
<dependency
org=
"org.apache.axis"
name=
"com.springsource.org.apache.axis"
rev=
"1.4.0"
conf=
"optional, axis->compile"
/>
conf=
"optional, axis->compile"
/>
<dependency
org=
"org.apache.commons"
name=
"com.springsource.org.apache.commons.httpclient"
rev=
"3.1.0"
conf=
"optional, httpclient->compile"
/>
conf=
"optional, httpclient->compile"
/>
<dependency
org=
"org.apache.commons"
name=
"com.springsource.org.apache.commons.logging"
rev=
"1.1.1"
conf=
"compile->compile"
/>
conf=
"compile->compile"
/>
<dependency
org=
"org.apache.log4j"
name=
"com.springsource.org.apache.log4j"
rev=
"1.2.15"
conf=
"optional, log4j->compile"
/>
conf=
"optional, log4j->compile"
/>
<dependency
org=
"org.codehaus.jackson"
name=
"com.springsource.org.codehaus.jackson.mapper"
rev=
"1.0.0"
conf=
"optional, jackson->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.aop"
rev=
"latest.integration"
conf=
"compile->compile"
/>
conf=
"compile->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.beans"
rev=
"latest.integration"
conf=
"compile->compile"
/>
conf=
"compile->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.context"
rev=
"latest.integration"
conf=
"compile->compile"
/>
conf=
"compile->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.core"
rev=
"latest.integration"
conf=
"compile->compile"
/>
conf=
"compile->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.oxm"
rev=
"latest.integration"
conf=
"
compile->compile"
/>
<!-- test dependencies -->
<dependency
org=
"org.apache.taglibs"
name=
"com.springsource.org.apache.taglibs.standard"
rev=
"1.1.2"
conf=
"test->runtime"
/>
conf=
"optional,
compile->compile"
/>
<!-- test dependencies -->
<dependency
org=
"org.apache.taglibs"
name=
"com.springsource.org.apache.taglibs.standard"
rev=
"1.1.2"
conf=
"test->runtime"
/>
<dependency
org=
"org.junit"
name=
"com.springsource.org.junit"
rev=
"4.5.0"
conf=
"test->runtime"
/>
<dependency
org=
"net.sourceforge.cglib"
name=
"com.springsource.net.sf.cglib"
rev=
"2.1.3"
conf=
"test->compile"
/>
<dependency
org=
"org.easymock"
name=
"com.springsource.org.easymock"
rev=
"2.3.0"
conf=
"test->compile"
/>
<dependency
org=
"org.mortbay.jetty"
name=
"com.springsource.org.mortbay.jetty.server"
rev=
"6.1.9"
conf=
"test->compile"
/>
conf=
"test->compile"
/>
<dependency
org=
"org.custommonkey.xmlunit"
name=
"com.springsource.org.custommonkey.xmlunit"
rev=
"1.2.0"
conf=
"test->compile"
/>
conf=
"test->compile"
/>
</dependencies>
</ivy-module>
org.springframework.web/src/main/java/org/springframework/http/converter/json/JacksonHttpMessageConverter.java
0 → 100644
浏览文件 @
3ca8c9be
/*
* 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.http.converter.json
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
org.codehaus.jackson.JsonEncoding
;
import
org.codehaus.jackson.JsonFactory
;
import
org.codehaus.jackson.JsonGenerator
;
import
org.codehaus.jackson.map.MappingJsonFactory
;
import
org.codehaus.jackson.map.ObjectMapper
;
import
org.springframework.http.HttpInputMessage
;
import
org.springframework.http.HttpOutputMessage
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.AbstractHttpMessageConverter
;
import
org.springframework.http.converter.HttpMessageNotReadableException
;
import
org.springframework.http.converter.HttpMessageNotWritableException
;
import
org.springframework.util.Assert
;
/**
* @author Arjen Poutsma
* @since 3.0
*/
public
class
JacksonHttpMessageConverter
<
T
>
extends
AbstractHttpMessageConverter
<
T
>
{
private
ObjectMapper
objectMapper
=
new
ObjectMapper
();
private
JsonFactory
jsonFactory
=
new
MappingJsonFactory
();
private
JsonEncoding
encoding
=
JsonEncoding
.
UTF8
;
public
JacksonHttpMessageConverter
()
{
super
(
new
MediaType
(
"application"
,
"json"
));
}
public
void
setObjectMapper
(
ObjectMapper
objectMapper
)
{
Assert
.
notNull
(
objectMapper
,
"'objectMapper' must not be null"
);
this
.
objectMapper
=
objectMapper
;
}
public
void
setJsonFactory
(
JsonFactory
jsonFactory
)
{
Assert
.
notNull
(
jsonFactory
,
"'jsonFactory' must not be null"
);
this
.
jsonFactory
=
jsonFactory
;
}
public
void
setEncoding
(
JsonEncoding
encoding
)
{
this
.
encoding
=
encoding
;
}
public
boolean
supports
(
Class
<?
extends
T
>
clazz
)
{
return
objectMapper
.
canSerialize
(
clazz
);
}
@Override
protected
T
readInternal
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
,
HttpMessageNotReadableException
{
return
objectMapper
.
readValue
(
inputMessage
.
getBody
(),
clazz
);
}
@Override
protected
MediaType
getContentType
(
T
t
)
{
Charset
charset
=
Charset
.
forName
(
encoding
.
getJavaName
());
return
new
MediaType
(
"application"
,
"json"
,
charset
);
}
@Override
protected
void
writeInternal
(
T
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
,
HttpMessageNotWritableException
{
JsonGenerator
jsonGenerator
=
jsonFactory
.
createJsonGenerator
(
outputMessage
.
getBody
(),
encoding
);
objectMapper
.
writeValue
(
jsonGenerator
,
t
);
}
}
org.springframework.web/src/main/java/org/springframework/http/converter/json/package-info.java
0 → 100644
浏览文件 @
3ca8c9be
/*
* 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.
*/
/**
*
* Provides a HttpMessageConverter implementations for handling JSON.
*
*/
package
org.springframework.http.converter.json
;
org.springframework.web/src/test/java/org/springframework/http/converter/json/JacksonHttpMessageConverterTest.java
0 → 100644
浏览文件 @
3ca8c9be
/*
* 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.http.converter.json
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
static
org
.
junit
.
Assert
.*;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.MockHttpInputMessage
;
import
org.springframework.http.MockHttpOutputMessage
;
/** @author Arjen Poutsma */
public
class
JacksonHttpMessageConverterTest
{
private
JacksonHttpMessageConverter
<
MyBean
>
converter
;
@Before
public
void
setUp
()
{
converter
=
new
JacksonHttpMessageConverter
<
MyBean
>();
}
@Test
public
void
read
()
throws
IOException
{
String
body
=
"{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}"
;
MockHttpInputMessage
inputMessage
=
new
MockHttpInputMessage
(
body
.
getBytes
(
"UTF-8"
));
inputMessage
.
getHeaders
().
setContentType
(
new
MediaType
(
"application"
,
"json"
));
MyBean
result
=
converter
.
read
(
MyBean
.
class
,
inputMessage
);
assertEquals
(
"Foo"
,
result
.
getString
());
assertEquals
(
42
,
result
.
getNumber
());
assertEquals
(
42
F
,
result
.
getFraction
(),
0
F
);
assertArrayEquals
(
new
String
[]{
"Foo"
,
"Bar"
},
result
.
getArray
());
assertTrue
(
result
.
isBool
());
assertArrayEquals
(
new
byte
[]{
0x1
,
0x2
},
result
.
getBytes
());
}
@Test
public
void
write
()
throws
IOException
{
MockHttpOutputMessage
outputMessage
=
new
MockHttpOutputMessage
();
MyBean
body
=
new
MyBean
();
body
.
setString
(
"Foo"
);
body
.
setNumber
(
42
);
body
.
setFraction
(
42
F
);
body
.
setArray
(
new
String
[]{
"Foo"
,
"Bar"
});
body
.
setBool
(
true
);
body
.
setBytes
(
new
byte
[]{
0x1
,
0x2
});
converter
.
write
(
body
,
outputMessage
);
Charset
utf8
=
Charset
.
forName
(
"UTF-8"
);
String
result
=
outputMessage
.
getBodyAsString
(
utf8
);
assertTrue
(
result
.
contains
(
"\"string\":\"Foo\""
));
assertTrue
(
result
.
contains
(
"\"number\":42"
));
assertTrue
(
result
.
contains
(
"fraction\":42.0"
));
assertTrue
(
result
.
contains
(
"\"array\":[\"Foo\",\"Bar\"]"
));
assertTrue
(
result
.
contains
(
"\"bool\":true"
));
assertTrue
(
result
.
contains
(
"\"bytes\":\"AQI=\""
));
assertEquals
(
"Invalid content-type"
,
new
MediaType
(
"application"
,
"json"
,
utf8
),
outputMessage
.
getHeaders
().
getContentType
());
}
public
static
class
MyBean
{
private
String
string
;
private
int
number
;
private
float
fraction
;
private
String
[]
array
;
private
boolean
bool
;
private
byte
[]
bytes
;
public
byte
[]
getBytes
()
{
return
bytes
;
}
public
void
setBytes
(
byte
[]
bytes
)
{
this
.
bytes
=
bytes
;
}
public
boolean
isBool
()
{
return
bool
;
}
public
void
setBool
(
boolean
bool
)
{
this
.
bool
=
bool
;
}
public
String
getString
()
{
return
string
;
}
public
void
setString
(
String
string
)
{
this
.
string
=
string
;
}
public
int
getNumber
()
{
return
number
;
}
public
void
setNumber
(
int
number
)
{
this
.
number
=
number
;
}
public
float
getFraction
()
{
return
fraction
;
}
public
void
setFraction
(
float
fraction
)
{
this
.
fraction
=
fraction
;
}
public
String
[]
getArray
()
{
return
array
;
}
public
void
setArray
(
String
[]
array
)
{
this
.
array
=
array
;
}
}
}
org.springframework.web/template.mf
浏览文件 @
3ca8c9be
...
...
@@ -4,6 +4,7 @@ Bundle-Vendor: SpringSource
Bundle-ManifestVersion: 2
Import-Template:
com.caucho.*;version="[3.1.5, 4.0.0)";resolution:=optional,
org.codehaus.jackson.*;version="[1.0.0, 1.1.0)";resolution:=optional,
com.sun.net.*;version="0";resolution:=optional,
javax.el.*;version="[1.0.0, 2.0.0)";resolution:=optional,
javax.faces.*;version="[1.1.0, 3.0.0)";resolution:=optional,
...
...
org.springframework.web/web.iml
浏览文件 @
3ca8c9be
...
...
@@ -245,6 +245,28 @@
</SOURCES>
</library>
</orderEntry>
<orderEntry
type=
"module-library"
>
<library>
<CLASSES>
<root
url=
"jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson.mapper/1.0.0/com.springsource.org.codehaus.jackson.mapper-1.0.0.jar!/"
/>
</CLASSES>
<JAVADOC
/>
<SOURCES>
<root
url=
"jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson.mapper/1.0.0/com.springsource.org.codehaus.jackson.mapper-sources-1.0.0.jar!/"
/>
</SOURCES>
</library>
</orderEntry>
<orderEntry
type=
"module-library"
>
<library>
<CLASSES>
<root
url=
"jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-1.0.0.jar!/"
/>
</CLASSES>
<JAVADOC
/>
<SOURCES>
<root
url=
"jar://$IVY_CACHE$/org.codehaus.jackson/com.springsource.org.codehaus.jackson/1.0.0/com.springsource.org.codehaus.jackson-sources-1.0.0.jar!/"
/>
</SOURCES>
</library>
</orderEntry>
</component>
<component
name=
"copyright"
>
<Base>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录