Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
带水的鱼儿
spring-framework
提交
f2be4e93
S
spring-framework
项目概览
带水的鱼儿
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
f2be4e93
编写于
7月 23, 2021
作者:
Y
Yanming Zhou
提交者:
Rossen Stoyanchev
7月 29, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use MessageSource for @ExceptionHandler methods
Follow-up for commit
bb816c12
See gh-27203
上级
5b3f11c5
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
72 addition
and
3 deletion
+72
-3
spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java
...in/java/org/springframework/web/method/HandlerMethod.java
+10
-1
spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java
...gframework/web/method/support/InvocableHandlerMethod.java
+9
-0
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
.../method/annotation/ExceptionHandlerExceptionResolver.java
+2
-2
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java
.../mvc/method/annotation/ServletInvocableHandlerMethod.java
+8
-0
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java
...od/annotation/ExceptionHandlerExceptionResolverTests.java
+43
-0
未找到文件。
spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java
浏览文件 @
f2be4e93
...
...
@@ -102,11 +102,20 @@ public class HandlerMethod {
* Create an instance from a bean instance and a method.
*/
public
HandlerMethod
(
Object
bean
,
Method
method
)
{
this
(
bean
,
method
,
null
);
}
/**
* Variant of {@link #HandlerMethod(Object, Method)} that
* also accepts a {@link MessageSource}.
*/
public
HandlerMethod
(
Object
bean
,
Method
method
,
@Nullable
MessageSource
messageSource
)
{
Assert
.
notNull
(
bean
,
"Bean is required"
);
Assert
.
notNull
(
method
,
"Method is required"
);
this
.
bean
=
bean
;
this
.
beanFactory
=
null
;
this
.
messageSource
=
null
;
this
.
messageSource
=
messageSource
;
this
.
beanType
=
ClassUtils
.
getUserClass
(
bean
);
this
.
method
=
method
;
this
.
bridgedMethod
=
BridgeMethodResolver
.
findBridgedMethod
(
method
);
...
...
spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java
浏览文件 @
f2be4e93
...
...
@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method
;
import
java.util.Arrays
;
import
org.springframework.context.MessageSource
;
import
org.springframework.core.CoroutinesUtils
;
import
org.springframework.core.DefaultParameterNameDiscoverer
;
import
org.springframework.core.KotlinDetector
;
...
...
@@ -71,6 +72,14 @@ public class InvocableHandlerMethod extends HandlerMethod {
super
(
bean
,
method
);
}
/**
* Variant of {@link #InvocableHandlerMethod(Object, Method)} that
* also accepts a {@link MessageSource}.
*/
public
InvocableHandlerMethod
(
Object
bean
,
Method
method
,
@Nullable
MessageSource
messageSource
)
{
super
(
bean
,
method
,
messageSource
);
}
/**
* Construct a new handler method with the given bean instance, method name and parameters.
* @param bean the object bean
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
浏览文件 @
f2be4e93
...
...
@@ -483,7 +483,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
}
Method
method
=
resolver
.
resolveMethod
(
exception
);
if
(
method
!=
null
)
{
return
new
ServletInvocableHandlerMethod
(
handlerMethod
.
getBean
(),
method
);
return
new
ServletInvocableHandlerMethod
(
handlerMethod
.
getBean
(),
method
,
this
.
applicationContext
);
}
// For advice applicability check below (involving base packages, assignable types
// and annotation presence), use target class instead of interface-based proxy.
...
...
@@ -498,7 +498,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
ExceptionHandlerMethodResolver
resolver
=
entry
.
getValue
();
Method
method
=
resolver
.
resolveMethod
(
exception
);
if
(
method
!=
null
)
{
return
new
ServletInvocableHandlerMethod
(
advice
.
resolveBean
(),
method
);
return
new
ServletInvocableHandlerMethod
(
advice
.
resolveBean
(),
method
,
this
.
applicationContext
);
}
}
}
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java
浏览文件 @
f2be4e93
...
...
@@ -25,6 +25,7 @@ import java.util.concurrent.Callable;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
org.springframework.context.MessageSource
;
import
org.springframework.core.KotlinDetector
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.core.ResolvableType
;
...
...
@@ -68,6 +69,13 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
@Nullable
private
HandlerMethodReturnValueHandlerComposite
returnValueHandlers
;
/**
* Variant of {@link #ServletInvocableHandlerMethod(Object, Method)} that
* also accepts a {@link MessageSource}.
*/
public
ServletInvocableHandlerMethod
(
Object
handler
,
Method
method
,
@Nullable
MessageSource
messageSource
)
{
super
(
handler
,
method
,
messageSource
);
}
/**
* Creates an instance from the given handler and method.
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java
浏览文件 @
f2be4e93
...
...
@@ -19,7 +19,9 @@ package org.springframework.web.servlet.mvc.method.annotation;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.io.Writer
;
import
java.net.SocketTimeoutException
;
import
java.util.Collections
;
import
java.util.Locale
;
import
org.junit.jupiter.api.BeforeAll
;
import
org.junit.jupiter.api.BeforeEach
;
...
...
@@ -30,8 +32,11 @@ import org.springframework.beans.FatalBeanException;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.i18n.LocaleContextHolder
;
import
org.springframework.context.support.StaticApplicationContext
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.server.ServerHttpRequest
;
...
...
@@ -42,6 +47,7 @@ import org.springframework.util.ClassUtils;
import
org.springframework.web.HttpRequestHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.context.support.WebApplicationObjectSupport
;
import
org.springframework.web.method.HandlerMethod
;
...
...
@@ -333,6 +339,28 @@ public class ExceptionHandlerExceptionResolverTests {
assertThat
(
this
.
response
.
getContentAsString
()).
isEqualTo
(
rootCause
.
toString
());
}
@Test
//gh-27156
void
resolveExceptionWithReasonResovledByMessageSource
()
throws
Exception
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
StaticApplicationContext
context
=
new
StaticApplicationContext
(
ctx
);
Locale
locale
=
Locale
.
ENGLISH
;
context
.
addMessage
(
"gateway.timeout"
,
locale
,
"Gateway Timeout"
);
context
.
refresh
();
LocaleContextHolder
.
setLocale
(
locale
);
this
.
resolver
.
setApplicationContext
(
context
);
this
.
resolver
.
afterPropertiesSet
();
SocketTimeoutException
ex
=
new
SocketTimeoutException
();
HandlerMethod
handlerMethod
=
new
HandlerMethod
(
new
ResponseBodyController
(),
"handle"
);
ModelAndView
mav
=
this
.
resolver
.
resolveException
(
this
.
request
,
this
.
response
,
handlerMethod
,
ex
);
assertThat
(
mav
).
as
(
"Exception was not handled"
).
isNotNull
();
assertThat
(
mav
.
isEmpty
()).
isTrue
();
assertThat
(
this
.
response
.
getStatus
()).
isEqualTo
(
HttpStatus
.
GATEWAY_TIMEOUT
.
value
());
assertThat
(
this
.
response
.
getErrorMessage
()).
isEqualTo
(
"Gateway Timeout"
);
assertThat
(
this
.
response
.
getContentAsString
()).
isEqualTo
(
""
);
}
@Test
void
resolveExceptionControllerAdviceHandler
()
throws
Exception
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
(
MyControllerAdviceConfig
.
class
);
...
...
@@ -530,6 +558,16 @@ public class ExceptionHandlerExceptionResolverTests {
}
}
@RestControllerAdvice
@Order
(
3
)
static
class
ResponseStatusTestExceptionResolver
{
@ExceptionHandler
(
SocketTimeoutException
.
class
)
@ResponseStatus
(
code
=
HttpStatus
.
GATEWAY_TIMEOUT
,
reason
=
"gateway.timeout"
)
public
void
handleException
(
SocketTimeoutException
ex
)
{
}
}
@Configuration
static
class
MyConfig
{
...
...
@@ -543,6 +581,11 @@ public class ExceptionHandlerExceptionResolverTests {
public
AnotherTestExceptionResolver
anotherTestExceptionResolver
()
{
return
new
AnotherTestExceptionResolver
();
}
@Bean
public
ResponseStatusTestExceptionResolver
responseStatusTestExceptionResolver
()
{
return
new
ResponseStatusTestExceptionResolver
();
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录