提交 39a97a61 编写于 作者: J Juergen Hoeller

resolve handler bean name early; validate header conditions as well

上级 aec2bc09
......@@ -170,6 +170,11 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
rawHandler = getDefaultHandler();
}
if (rawHandler != null) {
// Bean name or resolved handler?
if (rawHandler instanceof String) {
String handlerName = (String) rawHandler;
rawHandler = getApplicationContext().getBean(handlerName);
}
validateHandler(rawHandler, request);
handler = buildPathExposingHandler(rawHandler, lookupPath, null);
}
......@@ -200,6 +205,11 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
// Direct match?
Object handler = this.handlerMap.get(urlPath);
if (handler != null) {
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
}
validateHandler(handler, request);
return buildPathExposingHandler(handler, urlPath, null);
}
......@@ -213,6 +223,11 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
}
if (bestPathMatch != null) {
handler = this.handlerMap.get(bestPathMatch);
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
handler = getApplicationContext().getBean(handlerName);
}
validateHandler(handler, request);
String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPathMatch, urlPath);
Map<String, String> uriTemplateVariables =
......@@ -248,11 +263,6 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
protected Object buildPathExposingHandler(
Object rawHandler, String pathWithinMapping, Map<String, String> uriTemplateVariables) {
// Bean name or resolved handler?
if (rawHandler instanceof String) {
String handlerName = (String) rawHandler;
rawHandler = getApplicationContext().getBean(handlerName);
}
HandlerExecutionChain chain = new HandlerExecutionChain(rawHandler);
chain.addInterceptor(new PathExposingHandlerInterceptor(pathWithinMapping));
if (!CollectionUtils.isEmpty(uriTemplateVariables)) {
......@@ -353,7 +363,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
* as value.
* @see #getDefaultHandler()
*/
public final Map getHandlerMap() {
public final Map<String, Object> getHandlerMap() {
return Collections.unmodifiableMap(this.handlerMap);
}
......
......@@ -30,6 +30,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -186,11 +187,9 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
*/
@Override
protected void validateHandler(Object handler, HttpServletRequest request) throws Exception {
Class handlerClass = (handler instanceof String ?
getApplicationContext().getType((String) handler) : handler.getClass());
RequestMapping mapping = this.cachedMappings.get(handlerClass);
RequestMapping mapping = this.cachedMappings.get(handler.getClass());
if (mapping == null) {
mapping = AnnotationUtils.findAnnotation(handlerClass, RequestMapping.class);
mapping = AnnotationUtils.findAnnotation(handler.getClass(), RequestMapping.class);
}
if (mapping != null) {
validateMapping(mapping, request);
......@@ -218,6 +217,13 @@ public class DefaultAnnotationHandlerMapping extends AbstractDetectingUrlHandler
if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) {
throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap());
}
String[] mappedHeaders = mapping.headers();
if (!ServletAnnotationMappingUtils.checkHeaders(mappedHeaders, request)) {
throw new ServletRequestBindingException("Header conditions \"" +
StringUtils.arrayToDelimitedString(mappedHeaders, ", ") +
"\" not met for actual request");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册