28
28
import org .springframework .core .MethodParameter ;
29
29
import org .springframework .core .annotation .AnnotatedElementUtils ;
30
30
import org .springframework .core .annotation .SynthesizingMethodParameter ;
31
+ import org .springframework .http .HttpStatus ;
31
32
import org .springframework .util .Assert ;
32
33
import org .springframework .util .ClassUtils ;
34
+ import org .springframework .web .bind .annotation .ResponseStatus ;
33
35
34
36
/**
35
37
* Encapsulates information about a handler method consisting of a
@@ -65,7 +67,11 @@ public class HandlerMethod {
65
67
66
68
private final MethodParameter [] parameters ;
67
69
68
- private final HandlerMethod resolvedFromHandlerMethod ;
70
+ private HttpStatus responseStatus ;
71
+
72
+ private String responseStatusReason ;
73
+
74
+ private HandlerMethod resolvedFromHandlerMethod ;
69
75
70
76
71
77
/**
@@ -80,7 +86,7 @@ public HandlerMethod(Object bean, Method method) {
80
86
this .method = method ;
81
87
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
82
88
this .parameters = initMethodParameters ();
83
- this . resolvedFromHandlerMethod = null ;
89
+ evaluateResponseStatus () ;
84
90
}
85
91
86
92
/**
@@ -96,7 +102,7 @@ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)
96
102
this .method = bean .getClass ().getMethod (methodName , parameterTypes );
97
103
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (this .method );
98
104
this .parameters = initMethodParameters ();
99
- this . resolvedFromHandlerMethod = null ;
105
+ evaluateResponseStatus () ;
100
106
}
101
107
102
108
/**
@@ -114,7 +120,7 @@ public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
114
120
this .method = method ;
115
121
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
116
122
this .parameters = initMethodParameters ();
117
- this . resolvedFromHandlerMethod = null ;
123
+ evaluateResponseStatus () ;
118
124
}
119
125
120
126
/**
@@ -128,6 +134,8 @@ protected HandlerMethod(HandlerMethod handlerMethod) {
128
134
this .method = handlerMethod .method ;
129
135
this .bridgedMethod = handlerMethod .bridgedMethod ;
130
136
this .parameters = handlerMethod .parameters ;
137
+ this .responseStatus = handlerMethod .responseStatus ;
138
+ this .responseStatusReason = handlerMethod .responseStatusReason ;
131
139
this .resolvedFromHandlerMethod = handlerMethod .resolvedFromHandlerMethod ;
132
140
}
133
141
@@ -143,6 +151,8 @@ private HandlerMethod(HandlerMethod handlerMethod, Object handler) {
143
151
this .method = handlerMethod .method ;
144
152
this .bridgedMethod = handlerMethod .bridgedMethod ;
145
153
this .parameters = handlerMethod .parameters ;
154
+ this .responseStatus = handlerMethod .responseStatus ;
155
+ this .responseStatusReason = handlerMethod .responseStatusReason ;
146
156
this .resolvedFromHandlerMethod = handlerMethod ;
147
157
}
148
158
@@ -158,15 +168,27 @@ private MethodParameter[] initMethodParameters() {
158
168
return result ;
159
169
}
160
170
171
+ private void evaluateResponseStatus () {
172
+ ResponseStatus annotation = getMethodAnnotation (ResponseStatus .class );
173
+ if (annotation == null ) {
174
+ annotation = AnnotatedElementUtils .findMergedAnnotation (getBeanType (), ResponseStatus .class );
175
+ }
176
+ if (annotation != null ) {
177
+ this .responseStatus = annotation .code ();
178
+ this .responseStatusReason = annotation .reason ();
179
+ }
180
+ }
181
+
182
+
161
183
/**
162
- * Returns the bean for this handler method.
184
+ * Return the bean for this handler method.
163
185
*/
164
186
public Object getBean () {
165
187
return this .bean ;
166
188
}
167
189
168
190
/**
169
- * Returns the method for this handler method.
191
+ * Return the method for this handler method.
170
192
*/
171
193
public Method getMethod () {
172
194
return this .method ;
@@ -190,18 +212,28 @@ protected Method getBridgedMethod() {
190
212
}
191
213
192
214
/**
193
- * Returns the method parameters for this handler method.
215
+ * Return the method parameters for this handler method.
194
216
*/
195
217
public MethodParameter [] getMethodParameters () {
196
218
return this .parameters ;
197
219
}
198
220
199
221
/**
200
- * Return the HandlerMethod from which this HandlerMethod instance was
201
- * resolved via {@link #createWithResolvedBean()}.
222
+ * Return the specified response status, if any.
223
+ * @since 4.3.8
224
+ * @see ResponseStatus#code()
202
225
*/
203
- public HandlerMethod getResolvedFromHandlerMethod () {
204
- return this .resolvedFromHandlerMethod ;
226
+ protected HttpStatus getResponseStatus () {
227
+ return this .responseStatus ;
228
+ }
229
+
230
+ /**
231
+ * Return the associated response status reason, if any.
232
+ * @since 4.3.8
233
+ * @see ResponseStatus#reason()
234
+ */
235
+ protected String getResponseStatusReason () {
236
+ return this .responseStatusReason ;
205
237
}
206
238
207
239
/**
@@ -219,14 +251,14 @@ public MethodParameter getReturnValueType(Object returnValue) {
219
251
}
220
252
221
253
/**
222
- * Returns {@code true} if the method return type is void, {@code false} otherwise.
254
+ * Return {@code true} if the method return type is void, {@code false} otherwise.
223
255
*/
224
256
public boolean isVoid () {
225
257
return Void .TYPE .equals (getReturnType ().getParameterType ());
226
258
}
227
259
228
260
/**
229
- * Returns a single annotation on the underlying method traversing its super methods
261
+ * Return a single annotation on the underlying method traversing its super methods
230
262
* if no annotation can be found on the given method itself.
231
263
* <p>Also supports <em>merged</em> composed annotations with attribute
232
264
* overrides as of Spring Framework 4.2.2.
@@ -248,6 +280,14 @@ public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationTyp
248
280
return AnnotatedElementUtils .hasAnnotation (this .method , annotationType );
249
281
}
250
282
283
+ /**
284
+ * Return the HandlerMethod from which this HandlerMethod instance was
285
+ * resolved via {@link #createWithResolvedBean()}.
286
+ */
287
+ public HandlerMethod getResolvedFromHandlerMethod () {
288
+ return this .resolvedFromHandlerMethod ;
289
+ }
290
+
251
291
/**
252
292
* If the provided instance contains a bean name rather than an object instance,
253
293
* the bean name is resolved before a {@link HandlerMethod} is created and returned.
0 commit comments