Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
6a5b2672
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,体验更适合开发者的 AI 搜索 >>
提交
6a5b2672
编写于
5月 31, 2015
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce alias for 'value' attribute in @ResponseStatus
Issue: SPR-11393
上级
c55486d5
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
55 addition
and
61 deletion
+55
-61
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/StatusAssertionTests.java
...mples/standalone/resultmatchers/StatusAssertionTests.java
+3
-9
spring-web/src/main/java/org/springframework/web/bind/annotation/ResponseStatus.java
...g/springframework/web/bind/annotation/ResponseStatus.java
+25
-9
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
...ervlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
+1
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.java
.../annotation/AnnotationMethodHandlerExceptionResolver.java
+1
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java
...rvlet/mvc/annotation/ResponseStatusExceptionResolver.java
+1
-1
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java
.../mvc/method/annotation/ServletInvocableHandlerMethod.java
+1
-1
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java
...et/config/annotation/WebMvcConfigurationSupportTests.java
+1
-1
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolverTests.java
...tation/AnnotationMethodHandlerExceptionResolverTests.java
+2
-2
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolverTests.java
.../mvc/annotation/ResponseStatusExceptionResolverTests.java
+2
-5
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java
...vlet/mvc/annotation/ServletAnnotationControllerTests.java
+2
-2
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterIntegrationTests.java
...otation/RequestMappingHandlerAdapterIntegrationTests.java
+3
-3
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java
...tation/ServletAnnotationControllerHandlerMethodTests.java
+1
-1
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java
...method/annotation/ServletInvocableHandlerMethodTests.java
+12
-25
未找到文件。
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/StatusAssertionTests.java
浏览文件 @
6a5b2672
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
5
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,7 +16,6 @@
package
org.springframework.test.web.servlet.samples.standalone.resultmatchers
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -38,12 +37,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
*/
public
class
StatusAssertionTests
{
private
MockMvc
mockMvc
;
@Before
public
void
setup
()
{
this
.
mockMvc
=
standaloneSetup
(
new
StatusController
()).
build
();
}
private
final
MockMvc
mockMvc
=
standaloneSetup
(
new
StatusController
()).
build
();
@Test
public
void
testStatusInt
()
throws
Exception
{
...
...
@@ -86,7 +80,7 @@ public class StatusAssertionTests {
}
@RequestMapping
(
"/badRequest"
)
@ResponseStatus
(
value
=
HttpStatus
.
BAD_REQUEST
,
reason
=
"Expired token"
)
@ResponseStatus
(
code
=
HttpStatus
.
BAD_REQUEST
,
reason
=
"Expired token"
)
public
@ResponseBody
void
badRequest
(){
}
...
...
spring-web/src/main/java/org/springframework/web/bind/annotation/ResponseStatus.java
浏览文件 @
6a5b2672
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -22,13 +22,18 @@ import java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
org.springframework.core.annotation.AliasFor
;
import
org.springframework.http.HttpStatus
;
/**
* Marks a method or exception class with the status code and reason that should be returned. The status code is applied
* to the HTTP response when the handler method is invoked, or whenever said exception is thrown.
* Marks a method or exception class with the status {@link #code} and
* {@link #reason} that should be returned.
*
* <p>The status code is applied to the HTTP response when the handler
* method is invoked, or whenever said exception is thrown.
*
* @author Arjen Poutsma
* @author Sam Brannen
* @see org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver
* @since 3.0
*/
...
...
@@ -38,16 +43,27 @@ import org.springframework.http.HttpStatus;
public
@interface
ResponseStatus
{
/**
* The status code to use for the response.
*
* Alias for {@link #code}.
*/
@AliasFor
(
attribute
=
"code"
)
HttpStatus
value
()
default
HttpStatus
.
INTERNAL_SERVER_ERROR
;
/**
* The status <em>code</em> to use for the response.
* <p>Default is {@link HttpStatus#INTERNAL_SERVER_ERROR}, which should
* typically be changed to something more appropriate.
* @since 4.2
* @see javax.servlet.http.HttpServletResponse#setStatus(int)
*/
HttpStatus
value
();
@AliasFor
(
attribute
=
"value"
)
HttpStatus
code
()
default
HttpStatus
.
INTERNAL_SERVER_ERROR
;
/**
* The reason to be used for the response. <p>If this element is not set, it will default to the standard status
* message for the status code. Note that due to the use of {@code HttpServletResponse.sendError(int, String)},
* the response will be considered complete and should not be written to any further.
* The <em>reason</em> to be used for the response.
* <p>If this attribute is not set, it will default to the standard status
* message for the status code. Note that due to the use of
* {@code HttpServletResponse.sendError(int, String)}, the response will be
* considered complete and should not be written to any further.
*
* @see javax.servlet.http.HttpServletResponse#sendError(int, String)
*/
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java
浏览文件 @
6a5b2672
...
...
@@ -917,7 +917,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
ResponseStatus
responseStatusAnn
=
AnnotationUtils
.
findAnnotation
(
handlerMethod
,
ResponseStatus
.
class
);
if
(
responseStatusAnn
!=
null
)
{
HttpStatus
responseStatus
=
responseStatusAnn
.
valu
e
();
HttpStatus
responseStatus
=
responseStatusAnn
.
cod
e
();
String
reason
=
responseStatusAnn
.
reason
();
if
(!
StringUtils
.
hasText
(
reason
))
{
webRequest
.
getResponse
().
setStatus
(
responseStatus
.
value
());
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.java
浏览文件 @
6a5b2672
...
...
@@ -379,7 +379,7 @@ public class AnnotationMethodHandlerExceptionResolver extends AbstractHandlerExc
ResponseStatus
responseStatusAnn
=
AnnotationUtils
.
findAnnotation
(
handlerMethod
,
ResponseStatus
.
class
);
if
(
responseStatusAnn
!=
null
)
{
HttpStatus
responseStatus
=
responseStatusAnn
.
valu
e
();
HttpStatus
responseStatus
=
responseStatusAnn
.
cod
e
();
String
reason
=
responseStatusAnn
.
reason
();
if
(!
StringUtils
.
hasText
(
reason
))
{
webRequest
.
getResponse
().
setStatus
(
responseStatus
.
value
());
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java
浏览文件 @
6a5b2672
...
...
@@ -95,7 +95,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes
protected
ModelAndView
resolveResponseStatus
(
ResponseStatus
responseStatus
,
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
Exception
ex
)
throws
Exception
{
int
statusCode
=
responseStatus
.
valu
e
().
value
();
int
statusCode
=
responseStatus
.
cod
e
().
value
();
String
reason
=
responseStatus
.
reason
();
if
(
this
.
messageSource
!=
null
)
{
reason
=
this
.
messageSource
.
getMessage
(
reason
,
null
,
reason
,
LocaleContextHolder
.
getLocale
());
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java
浏览文件 @
6a5b2672
...
...
@@ -84,7 +84,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
private
void
initResponseStatus
()
{
ResponseStatus
annotation
=
getMethodAnnotation
(
ResponseStatus
.
class
);
if
(
annotation
!=
null
)
{
this
.
responseStatus
=
annotation
.
valu
e
();
this
.
responseStatus
=
annotation
.
cod
e
();
this
.
responseReason
=
annotation
.
reason
();
}
}
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupportTests.java
浏览文件 @
6a5b2672
...
...
@@ -372,7 +372,7 @@ public class WebMvcConfigurationSupportTests {
}
@ResponseStatus
(
value
=
HttpStatus
.
BAD_REQUEST
,
reason
=
"exception.user.exists"
)
@ResponseStatus
(
code
=
HttpStatus
.
BAD_REQUEST
,
reason
=
"exception.user.exists"
)
@SuppressWarnings
(
"serial"
)
public
static
class
UserAlreadyExistsException
extends
RuntimeException
{
}
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerExceptionResolverTests.java
浏览文件 @
6a5b2672
/*
* Copyright 2002-201
2
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -166,7 +166,7 @@ public class AnnotationMethodHandlerExceptionResolverTests {
}
@ExceptionHandler
(
SocketException
.
class
)
@ResponseStatus
(
valu
e
=
HttpStatus
.
NOT_ACCEPTABLE
,
reason
=
"This is simply unacceptable!"
)
@ResponseStatus
(
cod
e
=
HttpStatus
.
NOT_ACCEPTABLE
,
reason
=
"This is simply unacceptable!"
)
public
String
handleSocketException
(
Exception
ex
,
HttpServletResponse
response
)
{
return
"Y:"
+
ClassUtils
.
getShortName
(
ex
.
getClass
());
}
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolverTests.java
浏览文件 @
6a5b2672
...
...
@@ -110,19 +110,16 @@ public class ResponseStatusExceptionResolverTests {
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
@SuppressWarnings
(
"serial"
)
private
static
class
StatusCodeException
extends
Exception
{
}
@ResponseStatus
(
valu
e
=
HttpStatus
.
GONE
,
reason
=
"You suck!"
)
@ResponseStatus
(
cod
e
=
HttpStatus
.
GONE
,
reason
=
"You suck!"
)
@SuppressWarnings
(
"serial"
)
private
static
class
StatusCodeAndReasonException
extends
Exception
{
}
@ResponseStatus
(
valu
e
=
HttpStatus
.
GONE
,
reason
=
"gone.reason"
)
@ResponseStatus
(
cod
e
=
HttpStatus
.
GONE
,
reason
=
"gone.reason"
)
@SuppressWarnings
(
"serial"
)
private
static
class
StatusCodeAndReasonMessageException
extends
Exception
{
}
}
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java
浏览文件 @
6a5b2672
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -2992,7 +2992,7 @@ public class ServletAnnotationControllerTests {
public
static
class
ResponseStatusController
{
@RequestMapping
(
"/something"
)
@ResponseStatus
(
valu
e
=
HttpStatus
.
CREATED
,
reason
=
"It's alive!"
)
@ResponseStatus
(
cod
e
=
HttpStatus
.
CREATED
,
reason
=
"It's alive!"
)
public
void
handle
(
Writer
writer
)
throws
IOException
{
writer
.
write
(
"something"
);
}
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapterIntegrationTests.java
浏览文件 @
6a5b2672
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -353,14 +353,14 @@ public class RequestMappingHandlerAdapterIntegrationTests {
return
"viewName"
;
}
@ResponseStatus
(
value
=
HttpStatus
.
ACCEPTED
)
@ResponseStatus
(
HttpStatus
.
ACCEPTED
)
@ResponseBody
public
String
handleRequestBody
(
@RequestBody
byte
[]
bytes
)
throws
Exception
{
String
requestBody
=
new
String
(
bytes
,
"UTF-8"
);
return
"Handled requestBody=["
+
requestBody
+
"]"
;
}
@ResponseStatus
(
value
=
HttpStatus
.
ACCEPTED
)
@ResponseStatus
(
code
=
HttpStatus
.
ACCEPTED
)
@ResponseBody
public
String
handleAndValidateRequestBody
(
@Valid
TestBean
modelAttr
,
Errors
errors
)
throws
Exception
{
return
"Error count ["
+
errors
.
getErrorCount
()
+
"]"
;
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletAnnotationControllerHandlerMethodTests.java
浏览文件 @
6a5b2672
...
...
@@ -2657,7 +2657,7 @@ public class ServletAnnotationControllerHandlerMethodTests extends AbstractServl
public
static
class
ResponseStatusController
{
@RequestMapping
(
"/something"
)
@ResponseStatus
(
valu
e
=
HttpStatus
.
CREATED
,
reason
=
"It's alive!"
)
@ResponseStatus
(
cod
e
=
HttpStatus
.
CREATED
,
reason
=
"It's alive!"
)
public
void
handle
(
Writer
writer
)
throws
IOException
{
writer
.
write
(
"something"
);
}
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethodTests.java
浏览文件 @
6a5b2672
...
...
@@ -16,9 +16,6 @@
package
org.springframework.web.servlet.mvc.method.annotation
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.*;
import
java.lang.reflect.Method
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
...
...
@@ -26,7 +23,6 @@ import java.util.List;
import
javax.servlet.http.HttpServletResponse
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.core.MethodParameter
;
...
...
@@ -50,37 +46,30 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandlerCom
import
org.springframework.web.method.support.ModelAndViewContainer
;
import
org.springframework.web.servlet.view.RedirectView
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.*;
/**
* Test fixture with {@link ServletInvocableHandlerMethod}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
*/
public
class
ServletInvocableHandlerMethodTests
{
private
HandlerMethodArgumentResolverComposite
argumentResolvers
;
private
final
HandlerMethodArgumentResolverComposite
argumentResolvers
=
new
HandlerMethodArgumentResolverComposite
()
;
private
HandlerMethodReturnValueHandlerComposite
returnValueHandlers
;
private
final
HandlerMethodReturnValueHandlerComposite
returnValueHandlers
=
new
HandlerMethodReturnValueHandlerComposite
()
;
private
ModelAndViewContainer
mavContainer
;
private
final
ModelAndViewContainer
mavContainer
=
new
ModelAndViewContainer
()
;
private
ServletWebRequest
webRequest
;
private
final
MockHttpServletRequest
request
=
new
MockHttpServletRequest
()
;
private
MockHttpServletRequest
request
;
private
final
MockHttpServletResponse
response
=
new
MockHttpServletResponse
()
;
private
MockHttpServletResponse
response
;
private
final
ServletWebRequest
webRequest
=
new
ServletWebRequest
(
this
.
request
,
this
.
response
)
;
@Before
public
void
setUp
()
throws
Exception
{
this
.
returnValueHandlers
=
new
HandlerMethodReturnValueHandlerComposite
();
this
.
argumentResolvers
=
new
HandlerMethodArgumentResolverComposite
();
this
.
mavContainer
=
new
ModelAndViewContainer
();
this
.
request
=
new
MockHttpServletRequest
();
this
.
response
=
new
MockHttpServletResponse
();
this
.
webRequest
=
new
ServletWebRequest
(
this
.
request
,
this
.
response
);
}
@Test
public
void
invokeAndHandle_VoidWithResponseStatus
()
throws
Exception
{
ServletInvocableHandlerMethod
handlerMethod
=
getHandlerMethod
(
new
Handler
(),
"responseStatus"
);
...
...
@@ -279,11 +268,11 @@ public class ServletInvocableHandlerMethodTests {
return
"view"
;
}
@ResponseStatus
(
value
=
HttpStatus
.
BAD_REQUEST
)
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
void
responseStatus
()
{
}
@ResponseStatus
(
valu
e
=
HttpStatus
.
BAD_REQUEST
,
reason
=
"400 Bad Request"
)
@ResponseStatus
(
cod
e
=
HttpStatus
.
BAD_REQUEST
,
reason
=
"400 Bad Request"
)
public
String
responseStatusWithReason
()
{
return
"foo"
;
}
...
...
@@ -299,7 +288,6 @@ public class ServletInvocableHandlerMethodTests {
}
}
@SuppressWarnings
(
"unused"
)
private
static
class
MethodLevelResponseBodyHandler
{
@ResponseBody
...
...
@@ -324,12 +312,11 @@ public class ServletInvocableHandlerMethodTests {
return
new
DeferredResult
<>();
}
public
ResponseEntity
handleRawType
()
{
public
ResponseEntity
<
Void
>
handleRawType
()
{
return
ResponseEntity
.
ok
().
build
();
}
}
@SuppressWarnings
(
"unused"
)
private
static
class
ExceptionRaisingReturnValueHandler
implements
HandlerMethodReturnValueHandler
{
@Override
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录