Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
8d5fc2bf
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,发现更多精彩内容 >>
提交
8d5fc2bf
编写于
1月 15, 2010
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6686 - @ResponseBody throws HttpMediaTypeNotAcceptableException if client accepts "*/*"
上级
f7952fcc
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
167 addition
and
12 deletion
+167
-12
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
...ervlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
+4
-1
org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
...ramework/http/converter/AbstractHttpMessageConverter.java
+26
-7
org.springframework.web/src/main/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java
...tp/converter/json/MappingJacksonHttpMessageConverter.java
+2
-2
org.springframework.web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java
...p/converter/xml/Jaxb2RootElementHttpMessageConverter.java
+2
-2
org.springframework.web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java
...rk/http/converter/ByteArrayHttpMessageConverterTests.java
+11
-0
org.springframework.web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java
...amework/http/converter/FormHttpMessageConverterTests.java
+13
-0
org.springframework.web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java
...ngframework/http/converter/HttpMessageConverterTests.java
+98
-0
org.springframework.web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java
...ework/http/converter/StringHttpMessageConverterTests.java
+11
-0
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
浏览文件 @
8d5fc2bf
...
...
@@ -864,6 +864,9 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
if
(
acceptedMediaTypes
.
isEmpty
())
{
acceptedMediaTypes
=
Collections
.
singletonList
(
MediaType
.
ALL
);
}
else
{
Collections
.
sort
(
acceptedMediaTypes
);
}
HttpOutputMessage
outputMessage
=
new
ServletServerHttpResponse
(
webRequest
.
getResponse
());
Class
<?>
returnValueType
=
returnValue
.
getClass
();
List
<
MediaType
>
allSupportedMediaTypes
=
new
ArrayList
<
MediaType
>();
...
...
@@ -872,7 +875,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
allSupportedMediaTypes
.
addAll
(
messageConverter
.
getSupportedMediaTypes
());
for
(
MediaType
acceptedMediaType
:
acceptedMediaTypes
)
{
if
(
messageConverter
.
canWrite
(
returnValueType
,
acceptedMediaType
))
{
messageConverter
.
write
(
returnValue
,
null
,
outputMessage
);
messageConverter
.
write
(
returnValue
,
acceptedMediaType
,
outputMessage
);
this
.
responseArgumentUsed
=
true
;
return
;
}
...
...
org.springframework.web/src/main/java/org/springframework/http/converter/AbstractHttpMessageConverter.java
浏览文件 @
8d5fc2bf
...
...
@@ -92,7 +92,26 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
* type.
*/
public
boolean
canRead
(
Class
<?
extends
T
>
clazz
,
MediaType
mediaType
)
{
return
supports
(
clazz
)
&&
isSupported
(
mediaType
);
return
supports
(
clazz
)
&&
canRead
(
mediaType
);
}
/**
* Returns true if any of the {@linkplain #setSupportedMediaTypes(List) supported media types} include the given media
* type.
*
* @param mediaType the media type
* @return true if the supported media types include the media type, or if the media type is {@code null}
*/
protected
boolean
canRead
(
MediaType
mediaType
)
{
if
(
mediaType
==
null
)
{
return
true
;
}
for
(
MediaType
supportedMediaType
:
getSupportedMediaTypes
())
{
if
(
supportedMediaType
.
includes
(
mediaType
))
{
return
true
;
}
}
return
false
;
}
/**
...
...
@@ -103,22 +122,22 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
* type.
*/
public
boolean
canWrite
(
Class
<?
extends
T
>
clazz
,
MediaType
mediaType
)
{
return
supports
(
clazz
)
&&
isSupported
(
mediaType
);
return
supports
(
clazz
)
&&
canWrite
(
mediaType
);
}
/**
* Returns true if
any of the {@linkplain #setSupportedMediaTypes(List) supported media types} include the given media
*
type
.
* Returns true if
the given media type includes any of the
*
{@linkplain #setSupportedMediaTypes(List) supported media types}
.
*
* @param mediaType the media type
* @return true if the supported media types include the media type, or if the media type is {@code null}
*/
protected
boolean
isSupported
(
MediaType
mediaType
)
{
protected
boolean
canWrite
(
MediaType
mediaType
)
{
if
(
mediaType
==
null
)
{
return
true
;
}
for
(
MediaType
supportedMediaType
:
getSupportedMediaTypes
())
{
if
(
supportedMediaType
.
includes
(
m
ediaType
))
{
if
(
mediaType
.
includes
(
supportedM
ediaType
))
{
return
true
;
}
}
...
...
@@ -165,7 +184,7 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
public
final
void
write
(
T
t
,
MediaType
contentType
,
HttpOutputMessage
outputMessage
)
throws
IOException
,
HttpMessageNotWritableException
{
HttpHeaders
headers
=
outputMessage
.
getHeaders
();
if
(
contentType
==
null
)
{
if
(
contentType
==
null
||
contentType
.
isWildcardType
()
||
contentType
.
isWildcardSubtype
()
)
{
contentType
=
getDefaultContentType
(
t
);
}
if
(
contentType
!=
null
)
{
...
...
org.springframework.web/src/main/java/org/springframework/http/converter/json/MappingJacksonHttpMessageConverter.java
浏览文件 @
8d5fc2bf
...
...
@@ -89,12 +89,12 @@ public class MappingJacksonHttpMessageConverter extends AbstractHttpMessageConve
@Override
public
boolean
canRead
(
Class
<?>
clazz
,
MediaType
mediaType
)
{
JavaType
javaType
=
TypeFactory
.
fromClass
(
clazz
);
return
objectMapper
.
canDeserialize
(
javaType
)
&&
isSupporte
d
(
mediaType
);
return
objectMapper
.
canDeserialize
(
javaType
)
&&
canRea
d
(
mediaType
);
}
@Override
public
boolean
canWrite
(
Class
<?>
clazz
,
MediaType
mediaType
)
{
return
objectMapper
.
canSerialize
(
clazz
)
&&
isSupported
(
mediaType
);
return
objectMapper
.
canSerialize
(
clazz
)
&&
canWrite
(
mediaType
);
}
@Override
...
...
org.springframework.web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java
浏览文件 @
8d5fc2bf
...
...
@@ -52,12 +52,12 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa
@Override
public
boolean
canRead
(
Class
<?>
clazz
,
MediaType
mediaType
)
{
return
(
clazz
.
isAnnotationPresent
(
XmlRootElement
.
class
)
||
clazz
.
isAnnotationPresent
(
XmlType
.
class
))
&&
isSupporte
d
(
mediaType
);
canRea
d
(
mediaType
);
}
@Override
public
boolean
canWrite
(
Class
<?>
clazz
,
MediaType
mediaType
)
{
return
AnnotationUtils
.
findAnnotation
(
clazz
,
XmlRootElement
.
class
)
!=
null
&&
isSupported
(
mediaType
);
return
AnnotationUtils
.
findAnnotation
(
clazz
,
XmlRootElement
.
class
)
!=
null
&&
canWrite
(
mediaType
);
}
@Override
...
...
org.springframework.web/src/test/java/org/springframework/http/converter/ByteArrayHttpMessageConverterTests.java
浏览文件 @
8d5fc2bf
...
...
@@ -36,6 +36,17 @@ public class ByteArrayHttpMessageConverterTests {
converter
=
new
ByteArrayHttpMessageConverter
();
}
@Test
public
void
canRead
()
{
assertTrue
(
converter
.
canRead
(
byte
[].
class
,
new
MediaType
(
"application"
,
"octet-stream"
)));
}
@Test
public
void
canWrite
()
{
assertTrue
(
converter
.
canWrite
(
byte
[].
class
,
new
MediaType
(
"application"
,
"octet-stream"
)));
assertTrue
(
converter
.
canWrite
(
byte
[].
class
,
MediaType
.
ALL
));
}
@Test
public
void
read
()
throws
IOException
{
byte
[]
body
=
new
byte
[]{
0x1
,
0x2
};
...
...
org.springframework.web/src/test/java/org/springframework/http/converter/FormHttpMessageConverterTests.java
浏览文件 @
8d5fc2bf
...
...
@@ -40,6 +40,19 @@ public class FormHttpMessageConverterTests {
converter
=
new
FormHttpMessageConverter
();
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
canRead
()
{
assertTrue
(
converter
.
canRead
((
Class
<?
extends
MultiValueMap
<
String
,
String
>>)
MultiValueMap
.
class
,
new
MediaType
(
"application"
,
"x-www-form-urlencoded"
)));
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
canWrite
()
{
assertTrue
(
converter
.
canWrite
((
Class
<?
extends
MultiValueMap
<
String
,
String
>>)
MultiValueMap
.
class
,
new
MediaType
(
"application"
,
"x-www-form-urlencoded"
)));
assertTrue
(
converter
.
canWrite
((
Class
<?
extends
MultiValueMap
<
String
,
String
>>)
MultiValueMap
.
class
,
MediaType
.
ALL
));
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
read
()
throws
Exception
{
...
...
org.springframework.web/src/test/java/org/springframework/http/converter/HttpMessageConverterTests.java
0 → 100644
浏览文件 @
8d5fc2bf
/*
* Copyright 2002-2010 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
;
import
java.io.IOException
;
import
org.junit.Before
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.*;
import
org.springframework.http.HttpInputMessage
;
import
org.springframework.http.HttpOutputMessage
;
import
org.springframework.http.MediaType
;
/**
* Test-case for AbstractHttpMessageConverter.
*
* @author Arjen Poutsma
*/
public
class
HttpMessageConverterTests
{
private
static
final
MediaType
MEDIA_TYPE
=
new
MediaType
(
"foo"
,
"bar"
);
@Test
public
void
canRead
()
{
AbstractHttpMessageConverter
<
MyType
>
converter
=
new
MyHttpMessageConverter
<
MyType
>(
MEDIA_TYPE
)
{
@Override
protected
boolean
supports
(
Class
<?
extends
MyType
>
clazz
)
{
return
MyType
.
class
.
equals
(
clazz
);
}
};
assertTrue
(
converter
.
canRead
(
MyType
.
class
,
MEDIA_TYPE
));
assertFalse
(
converter
.
canRead
(
MyType
.
class
,
new
MediaType
(
"foo"
,
"*"
)));
assertFalse
(
converter
.
canRead
(
MyType
.
class
,
MediaType
.
ALL
));
}
@Test
public
void
canWrite
()
{
AbstractHttpMessageConverter
<
MyType
>
converter
=
new
MyHttpMessageConverter
<
MyType
>(
MEDIA_TYPE
)
{
@Override
protected
boolean
supports
(
Class
<?
extends
MyType
>
clazz
)
{
return
MyType
.
class
.
equals
(
clazz
);
}
};
assertTrue
(
converter
.
canWrite
(
MyType
.
class
,
MEDIA_TYPE
));
assertTrue
(
converter
.
canWrite
(
MyType
.
class
,
new
MediaType
(
"foo"
,
"*"
)));
assertTrue
(
converter
.
canWrite
(
MyType
.
class
,
MediaType
.
ALL
));
}
private
static
class
MyHttpMessageConverter
<
T
>
extends
AbstractHttpMessageConverter
<
T
>
{
private
MyHttpMessageConverter
(
MediaType
supportedMediaType
)
{
super
(
supportedMediaType
);
}
@Override
protected
boolean
supports
(
Class
<?
extends
T
>
clazz
)
{
fail
(
"Not expected"
);
return
false
;
}
@Override
protected
T
readInternal
(
Class
<
T
>
clazz
,
HttpInputMessage
inputMessage
)
throws
IOException
,
HttpMessageNotReadableException
{
fail
(
"Not expected"
);
return
null
;
}
@Override
protected
void
writeInternal
(
T
t
,
HttpOutputMessage
outputMessage
)
throws
IOException
,
HttpMessageNotWritableException
{
fail
(
"Not expected"
);
}
}
private
static
class
MyType
{
}
}
org.springframework.web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java
浏览文件 @
8d5fc2bf
...
...
@@ -37,6 +37,17 @@ public class StringHttpMessageConverterTests {
converter
=
new
StringHttpMessageConverter
();
}
@Test
public
void
canRead
()
{
assertTrue
(
converter
.
canRead
(
String
.
class
,
new
MediaType
(
"text"
,
"plain"
)));
}
@Test
public
void
canWrite
()
{
assertTrue
(
converter
.
canWrite
(
String
.
class
,
new
MediaType
(
"text"
,
"plain"
)));
assertTrue
(
converter
.
canWrite
(
String
.
class
,
MediaType
.
ALL
));
}
@Test
public
void
read
()
throws
IOException
{
String
body
=
"Hello World"
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录