11/*
2- * Copyright 2002-2020 the original author or authors.
2+ * Copyright 2002-2021 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2929import org .apache .commons .logging .LogFactory ;
3030
3131import org .springframework .beans .factory .BeanFactory ;
32+ import org .springframework .context .MessageSource ;
33+ import org .springframework .context .i18n .LocaleContextHolder ;
3234import org .springframework .core .BridgeMethodResolver ;
3335import org .springframework .core .MethodParameter ;
3436import org .springframework .core .ResolvableType ;
@@ -70,6 +72,9 @@ public class HandlerMethod {
7072 @ Nullable
7173 private final BeanFactory beanFactory ;
7274
75+ @ Nullable
76+ private final MessageSource messageSource ;
77+
7378 private final Class <?> beanType ;
7479
7580 private final Method method ;
@@ -101,6 +106,7 @@ public HandlerMethod(Object bean, Method method) {
101106 Assert .notNull (method , "Method is required" );
102107 this .bean = bean ;
103108 this .beanFactory = null ;
109+ this .messageSource = null ;
104110 this .beanType = ClassUtils .getUserClass (bean );
105111 this .method = method ;
106112 this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
@@ -118,6 +124,7 @@ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)
118124 Assert .notNull (methodName , "Method name is required" );
119125 this .bean = bean ;
120126 this .beanFactory = null ;
127+ this .messageSource = null ;
121128 this .beanType = ClassUtils .getUserClass (bean );
122129 this .method = bean .getClass ().getMethod (methodName , parameterTypes );
123130 this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (this .method );
@@ -132,11 +139,23 @@ public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes)
132139 * re-create the {@code HandlerMethod} with an initialized bean.
133140 */
134141 public HandlerMethod (String beanName , BeanFactory beanFactory , Method method ) {
142+ this (beanName , beanFactory , null , method );
143+ }
144+
145+ /**
146+ * Variant of {@link #HandlerMethod(String, BeanFactory, Method)} that
147+ * also accepts a {@link MessageSource}.
148+ */
149+ public HandlerMethod (
150+ String beanName , BeanFactory beanFactory ,
151+ @ Nullable MessageSource messageSource , Method method ) {
152+
135153 Assert .hasText (beanName , "Bean name is required" );
136154 Assert .notNull (beanFactory , "BeanFactory is required" );
137155 Assert .notNull (method , "Method is required" );
138156 this .bean = beanName ;
139157 this .beanFactory = beanFactory ;
158+ this .messageSource = messageSource ;
140159 Class <?> beanType = beanFactory .getType (beanName );
141160 if (beanType == null ) {
142161 throw new IllegalStateException ("Cannot resolve bean type for bean with name '" + beanName + "'" );
@@ -156,6 +175,7 @@ protected HandlerMethod(HandlerMethod handlerMethod) {
156175 Assert .notNull (handlerMethod , "HandlerMethod is required" );
157176 this .bean = handlerMethod .bean ;
158177 this .beanFactory = handlerMethod .beanFactory ;
178+ this .messageSource = handlerMethod .messageSource ;
159179 this .beanType = handlerMethod .beanType ;
160180 this .method = handlerMethod .method ;
161181 this .bridgedMethod = handlerMethod .bridgedMethod ;
@@ -174,6 +194,7 @@ private HandlerMethod(HandlerMethod handlerMethod, Object handler) {
174194 Assert .notNull (handler , "Handler object is required" );
175195 this .bean = handler ;
176196 this .beanFactory = handlerMethod .beanFactory ;
197+ this .messageSource = handlerMethod .messageSource ;
177198 this .beanType = handlerMethod .beanType ;
178199 this .method = handlerMethod .method ;
179200 this .bridgedMethod = handlerMethod .bridgedMethod ;
@@ -199,8 +220,13 @@ private void evaluateResponseStatus() {
199220 annotation = AnnotatedElementUtils .findMergedAnnotation (getBeanType (), ResponseStatus .class );
200221 }
201222 if (annotation != null ) {
223+ String reason = annotation .reason ();
224+ String resolvedReason = (StringUtils .hasText (reason ) && this .messageSource != null ?
225+ this .messageSource .getMessage (reason , null , reason , LocaleContextHolder .getLocale ()) :
226+ reason );
227+
202228 this .responseStatus = annotation .code ();
203- this .responseStatusReason = annotation . reason () ;
229+ this .responseStatusReason = resolvedReason ;
204230 }
205231 }
206232
0 commit comments