Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
04fa5d4b
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,发现更多精彩内容 >>
提交
04fa5d4b
编写于
9月 25, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6093 - MVC Annotation Inheritance
上级
d8245c80
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
57 addition
and
40 deletion
+57
-40
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
...ervlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
+8
-8
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.java
.../annotation/AnnotationMethodHandlerExceptionResolver.java
+28
-31
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolverTests.java
...tation/AnnotationMethodHandlerExceptionResolverTests.java
+21
-1
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
浏览文件 @
04fa5d4b
...
@@ -341,7 +341,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
...
@@ -341,7 +341,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
public
ModelAndView
handle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
public
ModelAndView
handle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
throws
Exception
{
if
(
handler
.
getClass
().
getAnnotation
(
SessionAttributes
.
class
)
!=
null
)
{
if
(
AnnotationUtils
.
findAnnotation
(
handler
.
getClass
(),
SessionAttributes
.
class
)
!=
null
)
{
// Always prevent caching in case of session attribute management.
// Always prevent caching in case of session attribute management.
checkAndPrepare
(
request
,
response
,
this
.
cacheSecondsForSessionAttributeHandlers
,
true
);
checkAndPrepare
(
request
,
response
,
this
.
cacheSecondsForSessionAttributeHandlers
,
true
);
// Prepare cached set of session attributes names.
// Prepare cached set of session attributes names.
...
@@ -706,9 +706,9 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
...
@@ -706,9 +706,9 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
Object
returnValue
,
Object
returnValue
,
ExtendedModelMap
implicitModel
,
ExtendedModelMap
implicitModel
,
ServletWebRequest
webRequest
)
throws
Exception
{
ServletWebRequest
webRequest
)
throws
Exception
{
if
(
handlerMethod
.
isAnnotationPresent
(
ResponseStatus
.
class
))
{
ResponseStatus
responseStatus
=
AnnotationUtils
.
findAnnotation
(
handlerMethod
,
ResponseStatus
.
class
);
ResponseStatus
responseStatus
=
handlerMethod
.
getAnnotation
(
ResponseStatus
.
class
);
if
(
responseStatus
!=
null
)
{
HttpServletResponse
response
=
webRequest
.
getResponse
();
HttpServletResponse
response
=
webRequest
.
getResponse
();
response
.
setStatus
(
responseStatus
.
value
().
value
());
response
.
setStatus
(
responseStatus
.
value
().
value
());
responseArgumentUsed
=
true
;
responseArgumentUsed
=
true
;
...
@@ -725,8 +725,8 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
...
@@ -725,8 +725,8 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
}
}
}
}
if
(
returnValue
!=
null
&&
handlerMethod
.
isAnnotationPresent
(
ResponseBody
.
class
)
)
{
if
(
returnValue
!=
null
&&
AnnotationUtils
.
findAnnotation
(
handlerMethod
,
ResponseBody
.
class
)
!=
null
)
{
handleRe
quest
Body
(
returnValue
,
webRequest
);
handleRe
sponse
Body
(
returnValue
,
webRequest
);
}
}
if
(
returnValue
instanceof
ModelAndView
)
{
if
(
returnValue
instanceof
ModelAndView
)
{
...
@@ -740,7 +740,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
...
@@ -740,7 +740,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
else
if
(
returnValue
instanceof
View
)
{
else
if
(
returnValue
instanceof
View
)
{
return
new
ModelAndView
((
View
)
returnValue
).
addAllObjects
(
implicitModel
);
return
new
ModelAndView
((
View
)
returnValue
).
addAllObjects
(
implicitModel
);
}
}
else
if
(
handlerMethod
.
isAnnotationPresent
(
ModelAttribute
.
class
)
)
{
else
if
(
AnnotationUtils
.
findAnnotation
(
handlerMethod
,
ModelAttribute
.
class
)
!=
null
)
{
addReturnValueAsModelAttribute
(
handlerMethod
,
handlerType
,
returnValue
,
implicitModel
);
addReturnValueAsModelAttribute
(
handlerMethod
,
handlerType
,
returnValue
,
implicitModel
);
return
new
ModelAndView
().
addAllObjects
(
implicitModel
);
return
new
ModelAndView
().
addAllObjects
(
implicitModel
);
}
}
...
@@ -771,7 +771,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
...
@@ -771,7 +771,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
private
void
handleRe
quest
Body
(
Object
returnValue
,
ServletWebRequest
webRequest
)
throws
ServletException
,
IOException
{
private
void
handleRe
sponse
Body
(
Object
returnValue
,
ServletWebRequest
webRequest
)
throws
ServletException
,
IOException
{
HttpInputMessage
inputMessage
=
new
ServletServerHttpRequest
(
webRequest
.
getRequest
());
HttpInputMessage
inputMessage
=
new
ServletServerHttpRequest
(
webRequest
.
getRequest
());
List
<
MediaType
>
acceptedMediaTypes
=
inputMessage
.
getHeaders
().
getAccept
();
List
<
MediaType
>
acceptedMediaTypes
=
inputMessage
.
getHeaders
().
getAccept
();
HttpOutputMessage
outputMessage
=
new
ServletServerHttpResponse
(
webRequest
.
getResponse
());
HttpOutputMessage
outputMessage
=
new
ServletServerHttpResponse
(
webRequest
.
getResponse
());
...
...
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.java
浏览文件 @
04fa5d4b
...
@@ -39,6 +39,7 @@ import javax.servlet.http.HttpSession;
...
@@ -39,6 +39,7 @@ import javax.servlet.http.HttpSession;
import
org.springframework.core.GenericTypeResolver
;
import
org.springframework.core.GenericTypeResolver
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.util.ObjectUtils
;
...
@@ -56,8 +57,8 @@ import org.springframework.web.servlet.support.RequestContextUtils;
...
@@ -56,8 +57,8 @@ import org.springframework.web.servlet.support.RequestContextUtils;
/**
/**
* Implementation of the {@link org.springframework.web.servlet.HandlerExceptionResolver} interface that handles
* Implementation of the {@link org.springframework.web.servlet.HandlerExceptionResolver} interface that handles
* exceptions through the {@link ExceptionHandler} annotation.
* exceptions through the {@link ExceptionHandler} annotation.
<p>This exception resolver is enabled by default in the
*
<p>This exception resolver is enabled by default in the
{@link org.springframework.web.servlet.DispatcherServlet}.
* {@link org.springframework.web.servlet.DispatcherServlet}.
*
*
* @author Arjen Poutsma
* @author Arjen Poutsma
* @since 3.0
* @since 3.0
...
@@ -110,7 +111,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -110,7 +111,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
/**
/**
* Finds the handler method that matches the thrown exception best.
* Finds the handler method that matches the thrown exception best.
*
*
* @param handler
the handler object
* @param handler the handler object
* @param thrownException the exception to be handled
* @param thrownException the exception to be handled
* @return the best matching method; or <code>null</code> if none is found
* @return the best matching method; or <code>null</code> if none is found
*/
*/
...
@@ -132,23 +133,25 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -132,23 +133,25 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
}
}
else
{
else
{
Method
oldMappedMethod
=
resolverMethods
.
get
(
handledException
);
Method
oldMappedMethod
=
resolverMethods
.
get
(
handledException
);
throw
new
IllegalStateException
(
if
(!
oldMappedMethod
.
equals
(
method
))
{
"Ambiguous exception handler mapped for "
+
handledException
+
"]: {"
+
throw
new
IllegalStateException
(
oldMappedMethod
+
", "
+
method
+
"}."
);
"Ambiguous exception handler mapped for "
+
handledException
+
"]: {"
+
oldMappedMethod
+
", "
+
method
+
"}."
);
}
}
}
}
}
}
}
}
}
});
});
return
getBestMatchingMethod
(
thrownException
,
resolverMethods
);
return
getBestMatchingMethod
(
thrownException
,
resolverMethods
);
}
}
/**
/**
* Returns all the exception classes handled by the given method.
* Returns all the exception classes handled by the given method. <p>Default implementation looks for exceptions in the
* <p>Default implementation looks for exceptions in the {@linkplain ExceptionHandler#value() annotation}, or -
* {@linkplain ExceptionHandler#value() annotation}, or - if that annotation element is empty - any exceptions listed
* if that annotation element is empty - any exceptions listed in the method parameters if the method is annotated
* in the method parameters if the method is annotated with {@code @ExceptionHandler}.
* with {@code @ExceptionHandler}.
*
*
* @param method the method
* @param method the method
* @return the handled exceptions
* @return the handled exceptions
...
@@ -156,7 +159,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -156,7 +159,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
protected
List
<
Class
<?
extends
Throwable
>>
getHandledExceptions
(
Method
method
)
{
protected
List
<
Class
<?
extends
Throwable
>>
getHandledExceptions
(
Method
method
)
{
List
<
Class
<?
extends
Throwable
>>
result
=
new
ArrayList
<
Class
<?
extends
Throwable
>>();
List
<
Class
<?
extends
Throwable
>>
result
=
new
ArrayList
<
Class
<?
extends
Throwable
>>();
ExceptionHandler
exceptionHandler
=
method
.
getAnnotation
(
ExceptionHandler
.
class
);
ExceptionHandler
exceptionHandler
=
AnnotationUtils
.
findAnnotation
(
method
,
ExceptionHandler
.
class
);
if
(
exceptionHandler
!=
null
)
{
if
(
exceptionHandler
!=
null
)
{
if
(!
ObjectUtils
.
isEmpty
(
exceptionHandler
.
value
()))
{
if
(!
ObjectUtils
.
isEmpty
(
exceptionHandler
.
value
()))
{
result
.
addAll
(
Arrays
.
asList
(
exceptionHandler
.
value
()));
result
.
addAll
(
Arrays
.
asList
(
exceptionHandler
.
value
()));
...
@@ -172,9 +175,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -172,9 +175,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
return
result
;
return
result
;
}
}
/**
/** Returns the best matching method. Uses the {@link DepthComparator}. */
* Returns the best matching method. Uses the {@link DepthComparator}.
*/
private
Method
getBestMatchingMethod
(
Exception
thrownException
,
private
Method
getBestMatchingMethod
(
Exception
thrownException
,
Map
<
Class
<?
extends
Throwable
>,
Method
>
resolverMethods
)
{
Map
<
Class
<?
extends
Throwable
>,
Method
>
resolverMethods
)
{
if
(!
resolverMethods
.
isEmpty
())
{
if
(!
resolverMethods
.
isEmpty
())
{
...
@@ -189,9 +190,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -189,9 +190,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
}
}
}
}
/**
/** Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}. */
* Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}.
*/
private
Object
[]
resolveHandlerArguments
(
Method
handlerMethod
,
private
Object
[]
resolveHandlerArguments
(
Method
handlerMethod
,
Object
handler
,
Object
handler
,
NativeWebRequest
webRequest
,
NativeWebRequest
webRequest
,
...
@@ -222,11 +221,11 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -222,11 +221,11 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
}
}
/**
/**
* Resolves common method arguments. Delegates to registered {@link #setCustomArgumentResolver(WebArgumentResolver)
argumentResolvers} first,
* Resolves common method arguments. Delegates to registered {@link #setCustomArgumentResolver(WebArgumentResolver)
* then checking {@link #resolveStandardArgument}.
*
argumentResolvers} first,
then checking {@link #resolveStandardArgument}.
*
*
* @param methodParameter the method parameter
* @param methodParameter the method parameter
* @param webRequest
the request
* @param webRequest the request
* @param thrownException the exception thrown
* @param thrownException the exception thrown
* @return the argument value, or {@link WebArgumentResolver#UNRESOLVED}
* @return the argument value, or {@link WebArgumentResolver#UNRESOLVED}
*/
*/
...
@@ -256,13 +255,13 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -256,13 +255,13 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
}
}
/**
/**
* Resolves standard method arguments. Default implementation handles {@link NativeWebRequest},
* Resolves standard method arguments. Default implementation handles {@link NativeWebRequest},
{@link ServletRequest},
* {@link ServletRe
quest}, {@link ServletResponse}, {@link HttpSession}, {@link Principal}, {@link Locale
},
* {@link ServletRe
sponse}, {@link HttpSession}, {@link Principal}, {@link Locale}, request {@link InputStream
},
* request {@link
InputStream}, request {@link Reader}, response {@link OutputStream}, response {@link Writer},
* request {@link
Reader}, response {@link OutputStream}, response {@link Writer}, and the given {@code
*
and the given {@code
thrownException}.
* thrownException}.
*
*
* @param parameterType
the method parameter type
* @param parameterType the method parameter type
* @param webRequest
the request
* @param webRequest the request
* @param thrownException the exception thrown
* @param thrownException the exception thrown
* @return the argument value, or {@link WebArgumentResolver#UNRESOLVED}
* @return the argument value, or {@link WebArgumentResolver#UNRESOLVED}
*/
*/
...
@@ -327,8 +326,8 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -327,8 +326,8 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
private
ModelAndView
getModelAndView
(
Method
handlerMethod
,
Object
returnValue
,
ServletWebRequest
webRequest
)
private
ModelAndView
getModelAndView
(
Method
handlerMethod
,
Object
returnValue
,
ServletWebRequest
webRequest
)
throws
Exception
{
throws
Exception
{
if
(
handlerMethod
.
isAnnotationPresent
(
ResponseStatus
.
class
))
{
ResponseStatus
responseStatus
=
AnnotationUtils
.
findAnnotation
(
handlerMethod
,
ResponseStatus
.
class
);
ResponseStatus
responseStatus
=
handlerMethod
.
getAnnotation
(
ResponseStatus
.
class
);
if
(
responseStatus
!=
null
)
{
HttpServletResponse
response
=
webRequest
.
getResponse
();
HttpServletResponse
response
=
webRequest
.
getResponse
();
response
.
setStatus
(
responseStatus
.
value
().
value
());
response
.
setStatus
(
responseStatus
.
value
().
value
());
}
}
...
@@ -355,9 +354,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
...
@@ -355,9 +354,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
}
}
}
}
/**
/** Comparator capable of sorting exceptions based on their depth from the thrown exception type. */
* Comparator capable of sorting exceptions based on their depth from the thrown exception type.
*/
private
static
class
DepthComparator
implements
Comparator
<
Class
<?
extends
Throwable
>>
{
private
static
class
DepthComparator
implements
Comparator
<
Class
<?
extends
Throwable
>>
{
private
final
Class
<?
extends
Throwable
>
handlerExceptionType
;
private
final
Class
<?
extends
Throwable
>
handlerExceptionType
;
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolverTests.java
浏览文件 @
04fa5d4b
...
@@ -64,7 +64,17 @@ public class AnnotationMethodHandlerExceptionResolverTests {
...
@@ -64,7 +64,17 @@ public class AnnotationMethodHandlerExceptionResolverTests {
assertEquals
(
"Invalid view name returned"
,
"BindException"
,
mav
.
getViewName
());
assertEquals
(
"Invalid view name returned"
,
"BindException"
,
mav
.
getViewName
());
assertEquals
(
"Invalid status code returned"
,
406
,
response
.
getStatus
());
assertEquals
(
"Invalid status code returned"
,
406
,
response
.
getStatus
());
}
}
@Test
public
void
inherited
()
{
IOException
ex
=
new
IOException
();
InheritedController
controller
=
new
InheritedController
();
ModelAndView
mav
=
exceptionResolver
.
resolveException
(
request
,
response
,
controller
,
ex
);
assertNotNull
(
"No ModelAndView returned"
,
mav
);
assertEquals
(
"Invalid view name returned"
,
"GenericError"
,
mav
.
getViewName
());
assertEquals
(
"Invalid status code returned"
,
500
,
response
.
getStatus
());
}
@Test
(
expected
=
IllegalStateException
.
class
)
@Test
(
expected
=
IllegalStateException
.
class
)
public
void
ambiguous
()
{
public
void
ambiguous
()
{
IllegalArgumentException
ex
=
new
IllegalArgumentException
();
IllegalArgumentException
ex
=
new
IllegalArgumentException
();
...
@@ -86,6 +96,7 @@ public class AnnotationMethodHandlerExceptionResolverTests {
...
@@ -86,6 +96,7 @@ public class AnnotationMethodHandlerExceptionResolverTests {
private
static
class
SimpleController
{
private
static
class
SimpleController
{
@ExceptionHandler
(
IOException
.
class
)
@ExceptionHandler
(
IOException
.
class
)
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
public
String
handleIOException
(
IOException
ex
,
HttpServletRequest
request
)
{
public
String
handleIOException
(
IOException
ex
,
HttpServletRequest
request
)
{
return
ClassUtils
.
getShortName
(
ex
.
getClass
());
return
ClassUtils
.
getShortName
(
ex
.
getClass
());
}
}
...
@@ -103,6 +114,15 @@ public class AnnotationMethodHandlerExceptionResolverTests {
...
@@ -103,6 +114,15 @@ public class AnnotationMethodHandlerExceptionResolverTests {
}
}
@Controller
private
static
class
InheritedController
extends
SimpleController
{
@Override
public
String
handleIOException
(
IOException
ex
,
HttpServletRequest
request
)
{
return
"GenericError"
;
}
}
@Controller
@Controller
private
static
class
AmbiguousController
{
private
static
class
AmbiguousController
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录