Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
25aef4d3
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,发现更多精彩内容 >>
提交
25aef4d3
编写于
5月 06, 2017
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ResponseStatusException reason is optional (with lazily constructed message)
Issue: SPR-15524
上级
edbf9fa7
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
69 addition
and
52 deletion
+69
-52
spring-web/src/main/java/org/springframework/web/server/ResponseStatusException.java
...g/springframework/web/server/ResponseStatusException.java
+29
-8
spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java
...ngframework/web/reactive/DispatcherHandlerErrorTests.java
+3
-6
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/InvocableHandlerMethodTests.java
...b/reactive/result/method/InvocableHandlerMethodTests.java
+5
-9
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java
.../result/method/RequestMappingInfoHandlerMappingTests.java
+8
-12
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java
...rvlet/mvc/annotation/ResponseStatusExceptionResolver.java
+13
-11
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolverTests.java
.../mvc/annotation/ResponseStatusExceptionResolverTests.java
+11
-6
未找到文件。
spring-web/src/main/java/org/springframework/web/server/ResponseStatusException.java
浏览文件 @
25aef4d3
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
7
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.
...
...
@@ -16,6 +16,7 @@
package
org.springframework.web.server
;
import
org.springframework.core.NestedExceptionUtils
;
import
org.springframework.core.NestedRuntimeException
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.util.Assert
;
...
...
@@ -24,6 +25,7 @@ import org.springframework.util.Assert;
* Base class for exceptions associated with specific HTTP response status codes.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 5.0
*/
@SuppressWarnings
(
"serial"
)
...
...
@@ -35,37 +37,56 @@ public class ResponseStatusException extends NestedRuntimeException {
/**
* Constructor with a response code and a reason to add to the exception
* Constructor with a response status.
* @param status the HTTP status (required)
*/
public
ResponseStatusException
(
HttpStatus
status
)
{
this
(
status
,
null
,
null
);
}
/**
* Constructor with a response status and a reason to add to the exception
* message as explanation.
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
*/
public
ResponseStatusException
(
HttpStatus
status
,
String
reason
)
{
this
(
status
,
reason
,
null
);
}
/**
* Constructor with a nested exception.
* Constructor with a response status and a reason to add to the exception
* message as explanation, as well as a nested exception.
* @param status the HTTP status (required)
* @param reason the associated reason (optional)
* @param cause a nested exception (optional)
*/
public
ResponseStatusException
(
HttpStatus
status
,
String
reason
,
Throwable
cause
)
{
super
(
"Request failure [status: "
+
status
+
", reason: \""
+
reason
+
"\"]"
,
cause
);
Assert
.
notNull
(
status
,
"'status' is required"
);
Assert
.
notNull
(
reason
,
"'reason' is required"
);
super
(
null
,
cause
);
Assert
.
notNull
(
status
,
"HttpStatus is required"
);
this
.
status
=
status
;
this
.
reason
=
reason
;
}
/**
* The HTTP status that fits the exception.
* The HTTP status that fits the exception
(never {@code null})
.
*/
public
HttpStatus
getStatus
()
{
return
this
.
status
;
}
/**
* The reason explaining the exception.
* The reason explaining the exception
(potentially {@code null} or empty)
.
*/
public
String
getReason
()
{
return
this
.
reason
;
}
@Override
public
String
getMessage
()
{
String
msg
=
"Response status "
+
this
.
status
+
(
this
.
reason
!=
null
?
" with reason \""
+
reason
+
"\""
:
""
);
return
NestedExceptionUtils
.
buildMessage
(
msg
,
getCause
());
}
}
spring-webflux/src/test/java/org/springframework/web/reactive/DispatcherHandlerErrorTests.java
浏览文件 @
25aef4d3
...
...
@@ -51,10 +51,8 @@ import org.springframework.web.server.handler.ExceptionHandlingWebHandler;
import
static
org
.
hamcrest
.
CoreMatchers
.
instanceOf
;
import
static
org
.
hamcrest
.
CoreMatchers
.
startsWith
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertSame
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
springframework
.
http
.
MediaType
.
APPLICATION_JSON
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
springframework
.
http
.
MediaType
.*;
/**
* Test the effect of exceptions at different stages of request processing by
...
...
@@ -87,8 +85,7 @@ public class DispatcherHandlerErrorTests {
StepVerifier
.
create
(
publisher
)
.
consumeErrorWith
(
error
->
{
assertThat
(
error
,
instanceOf
(
ResponseStatusException
.
class
));
assertThat
(
error
.
getMessage
(),
is
(
"Request failure [status: 404, reason: \"No matching handler\"]"
));
assertThat
(
error
.
getMessage
(),
is
(
"Response status 404 with reason \"No matching handler\""
));
})
.
verify
();
}
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/InvocableHandlerMethodTests.java
浏览文件 @
25aef4d3
...
...
@@ -32,15 +32,11 @@ import org.springframework.web.reactive.BindingContext;
import
org.springframework.web.reactive.HandlerResult
;
import
org.springframework.web.server.UnsupportedMediaTypeStatusException
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
hamcrest
.
Matchers
.*;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.
any
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
springframework
.
web
.
method
.
ResolvableMethod
.
on
;
import
static
org
.
mockito
.
Mockito
.*;
import
static
org
.
springframework
.
web
.
method
.
ResolvableMethod
.*;
/**
* Unit tests for {@link InvocableHandlerMethod}.
...
...
@@ -109,7 +105,7 @@ public class InvocableHandlerMethodTests {
fail
(
"Expected UnsupportedMediaTypeStatusException"
);
}
catch
(
UnsupportedMediaTypeStatusException
ex
)
{
assertThat
(
ex
.
getMessage
(),
is
(
"Re
quest failure [status: 415, reason: \"boo\"]
"
));
assertThat
(
ex
.
getMessage
(),
is
(
"Re
sponse status 415 with reason \"boo\"
"
));
}
}
...
...
spring-webflux/src/test/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMappingTests.java
浏览文件 @
25aef4d3
...
...
@@ -48,7 +48,7 @@ import org.springframework.web.method.HandlerMethod;
import
org.springframework.web.reactive.BindingContext
;
import
org.springframework.web.reactive.HandlerMapping
;
import
org.springframework.web.reactive.HandlerResult
;
import
org.springframework.web.reactive.result.method.RequestMappingInfo.
BuilderConfiguration
;
import
org.springframework.web.reactive.result.method.RequestMappingInfo.
*
;
import
org.springframework.web.server.MethodNotAllowedException
;
import
org.springframework.web.server.NotAcceptableStatusException
;
import
org.springframework.web.server.ServerWebExchange
;
...
...
@@ -56,16 +56,13 @@ import org.springframework.web.server.ServerWebInputException;
import
org.springframework.web.server.UnsupportedMediaTypeStatusException
;
import
org.springframework.web.server.support.HttpRequestPathHelper
;
import
static
org
.
hamcrest
.
CoreMatchers
.
containsString
;
import
static
org
.
hamcrest
.
CoreMatchers
.
*
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
springframework
.
mock
.
http
.
server
.
reactive
.
test
.
MockServerHttpRequest
.
get
;
import
static
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMethod
.
GET
;
import
static
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMethod
.
HEAD
;
import
static
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMethod
.
OPTIONS
;
import
static
org
.
springframework
.
web
.
method
.
MvcAnnotationPredicates
.
getMapping
;
import
static
org
.
springframework
.
web
.
method
.
MvcAnnotationPredicates
.
requestMapping
;
import
static
org
.
springframework
.
web
.
method
.
ResolvableMethod
.
on
;
import
static
org
.
springframework
.
web
.
reactive
.
result
.
method
.
RequestMappingInfo
.
paths
;
import
static
org
.
springframework
.
mock
.
http
.
server
.
reactive
.
test
.
MockServerHttpRequest
.*;
import
static
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMethod
.*;
import
static
org
.
springframework
.
web
.
method
.
MvcAnnotationPredicates
.*;
import
static
org
.
springframework
.
web
.
method
.
ResolvableMethod
.*;
import
static
org
.
springframework
.
web
.
reactive
.
result
.
method
.
RequestMappingInfo
.*;
/**
* Unit tests for {@link RequestMappingInfoHandlerMapping}.
...
...
@@ -165,8 +162,7 @@ public class RequestMappingInfoHandlerMappingTests {
Mono
<
Object
>
mono
=
this
.
handlerMapping
.
getHandler
(
exchange
);
assertError
(
mono
,
UnsupportedMediaTypeStatusException
.
class
,
ex
->
assertEquals
(
"Request failure [status: 415, "
+
"reason: \"Invalid mime type \"bogus\": does not contain '/'\"]"
,
ex
->
assertEquals
(
"Response status 415 with reason \"Invalid mime type \"bogus\": does not contain '/'\""
,
ex
.
getMessage
()));
}
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java
浏览文件 @
25aef4d3
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
7
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.
...
...
@@ -49,7 +49,8 @@ import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.0
* @see AnnotatedElementUtils#findMergedAnnotation
* @see ResponseStatus
* @see ResponseStatusException
*/
public
class
ResponseStatusExceptionResolver
extends
AbstractHandlerExceptionResolver
implements
MessageSourceAware
{
...
...
@@ -99,9 +100,8 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
* @param ex the exception
* @return an empty ModelAndView, i.e. exception resolved
*/
protected
ModelAndView
resolveResponseStatus
(
ResponseStatus
responseStatus
,
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
Exception
ex
)
throws
Exception
{
protected
ModelAndView
resolveResponseStatus
(
ResponseStatus
responseStatus
,
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
Exception
ex
)
throws
Exception
{
int
statusCode
=
responseStatus
.
code
().
value
();
String
reason
=
responseStatus
.
reason
();
...
...
@@ -125,8 +125,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
int
statusCode
=
ex
.
getStatus
().
value
();
String
reason
=
ex
.
getReason
();
applyStatusAndReason
(
statusCode
,
reason
,
response
);
return
new
ModelAndView
();
return
applyStatusAndReason
(
statusCode
,
reason
,
response
);
}
/**
...
...
@@ -135,19 +134,22 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
* {@link HttpServletResponse#sendError(int)} or
* {@link HttpServletResponse#sendError(int, String)} if there is a reason
* and then returns an empty ModelAndView.
* @param statusCode the HTTP status code
* @param reason the associated reason (may be {@code null} or empty)
* @param response current HTTP response
* @since 5.0
*/
protected
ModelAndView
applyStatusAndReason
(
int
statusCode
,
String
reason
,
HttpServletResponse
response
)
throws
IOException
{
if
(
this
.
messageSource
!=
null
)
{
reason
=
this
.
messageSource
.
getMessage
(
reason
,
null
,
reason
,
LocaleContextHolder
.
getLocale
());
}
if
(!
StringUtils
.
hasLength
(
reason
))
{
response
.
sendError
(
statusCode
);
}
else
{
response
.
sendError
(
statusCode
,
reason
);
String
resolvedReason
=
(
this
.
messageSource
!=
null
?
this
.
messageSource
.
getMessage
(
reason
,
null
,
reason
,
LocaleContextHolder
.
getLocale
())
:
reason
);
response
.
sendError
(
statusCode
,
resolvedReason
);
}
return
new
ModelAndView
();
}
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolverTests.java
浏览文件 @
25aef4d3
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
7
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.
...
...
@@ -111,23 +111,28 @@ public class ResponseStatusExceptionResolverTests {
Exception
cause
=
new
StatusCodeAndReasonMessageException
();
TypeMismatchException
ex
=
new
TypeMismatchException
(
"value"
,
ITestBean
.
class
,
cause
);
ModelAndView
mav
=
exceptionResolver
.
resolveException
(
request
,
response
,
null
,
ex
);
assertResolved
(
mav
,
410
,
null
);
assertResolved
(
mav
,
410
,
"gone.reason"
);
}
@Test
public
void
responseStatusException
()
throws
Exception
{
ResponseStatusException
ex
=
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"The reason"
);
ResponseStatusException
ex
=
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
);
ModelAndView
mav
=
exceptionResolver
.
resolveException
(
request
,
response
,
null
,
ex
);
assertResolved
(
mav
,
400
,
null
);
}
@Test
// SPR-15524
public
void
responseStatusExceptionWithReason
()
throws
Exception
{
ResponseStatusException
ex
=
new
ResponseStatusException
(
HttpStatus
.
BAD_REQUEST
,
"The reason"
);
ModelAndView
mav
=
exceptionResolver
.
resolveException
(
request
,
response
,
null
,
ex
);
assertResolved
(
mav
,
400
,
"The reason"
);
}
private
void
assertResolved
(
ModelAndView
mav
,
int
status
,
String
reason
)
{
assertTrue
(
"No Empty ModelAndView returned"
,
mav
!=
null
&&
mav
.
isEmpty
());
assertEquals
(
status
,
response
.
getStatus
());
if
(
reason
!=
null
)
{
assertEquals
(
reason
,
response
.
getErrorMessage
());
}
assertEquals
(
reason
,
response
.
getErrorMessage
());
assertTrue
(
response
.
isCommitted
());
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录