@Bean public DefaultAnnotationHandlerMapping mapping() { DefaultAnnotationHandlerMapping m = new DefaultAnnotationHandlerMapping(); m.setDetectHandlersInAncestorContexts(true); return m; }
@Autowired // org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping private DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping; public void outputMappings() { for (Map.Entry<String, Object> entry : defaultAnnotationHandlerMapping.getHandlerMap().entrySet()) { System.out.println(entry.getKey() + "->" + entry.getValue().getClass().getName()); } }
@Override public void setServletContext(ServletContext servletContext) { for (DefaultAnnotationHandlerMapping handlerMapping : handlerMappings) { handlerMapping.setPathMatcher(pathMatcher); } handlerAdapter.setPathMatcher(pathMatcher); } }
@Bean public DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping() { DefaultAnnotationHandlerMapping defaultAnnotationHandlerMapping = new DefaultAnnotationHandlerMapping(); defaultAnnotationHandlerMapping.setOrder(-1); // and other properties return defaultAnnotationHandlerMapping; }
private void findResources() { Map<String, Object> handlerMap = new HashMap<String, Object>(); DefaultAnnotationHandlerMapping annotationMapping = new DefaultAnnotationHandlerMapping(); annotationMapping.setApplicationContext(applicationContext); annotationMapping.initApplicationContext(); handlerMap.putAll(annotationMapping.getHandlerMap()); BeanNameUrlHandlerMapping beanMapping = new BeanNameUrlHandlerMapping(); beanMapping.setApplicationContext(applicationContext); beanMapping.initApplicationContext(); handlerMap.putAll(beanMapping.getHandlerMap()); this.urls = findUniqueUrls(handlerMap.keySet()); this.defaultResources = findMethods(handlerMap, this.urls); this.jsonResources = new ArrayList<ResourceInfo>(); for (Iterator<ResourceInfo> iterator = this.defaultResources.iterator(); iterator.hasNext();) { ResourceInfo info = (ResourceInfo) iterator.next(); if (info.getUrl().endsWith(".json")) { iterator.remove(); this.jsonResources.add(info); } } }
ApplicationContext context = getApplicationContext(); Class<?> handlerType = context.getType(beanName); RequestMapping mapping = context.findAnnotationOnBean(beanName, RequestMapping.class); if (typeLevelPatterns.length > 0) { String[] methodLevelPatterns = determineUrlsForHandlerMethods(handlerType, true); for (String typeLevelPattern : typeLevelPatterns) { if (!typeLevelPattern.startsWith("/")) { String combinedPattern = getPathMatcher().combine(typeLevelPattern, methodLevelPattern); addUrlsForPath(urls, combinedPattern); addUrlsForPath(urls, typeLevelPattern); return determineUrlsForHandlerMethods(handlerType, false); return determineUrlsForHandlerMethods(handlerType, false);
/** * Passes the given request to a proper controller. * * @param request * @return * @throws Exception */ public MockHttpServletResponse handle(HttpServletRequest request) throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); HandlerExecutionChain handlerExecutionChain = null; for (DefaultAnnotationHandlerMapping handlerMapping : handlerMappings) { handlerMapping.setPathMatcher(pathMatcher); handlerExecutionChain = handlerMapping.getHandler(request); if (handlerExecutionChain != null) { break; } } Assert.assertNotNull("The request URI does not exist", handlerExecutionChain); handlerAdapter.setPathMatcher(pathMatcher); handlerAdapter.handle(request, response, handlerExecutionChain.getHandler()); return response; }
protected String[] determineUrlsForHandler(String s) { return getFiltered(super.determineUrlsForHandler(s)); } }
@Override public void doWith(Method method) { RequestMapping mapping = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (mapping != null) { String[] mappedPatterns = mapping.value(); if (mappedPatterns.length > 0) { for (String mappedPattern : mappedPatterns) { if (!hasTypeLevelMapping && !mappedPattern.startsWith("/")) { mappedPattern = "/" + mappedPattern; } addUrlsForPath(urls, mappedPattern); } } else if (hasTypeLevelMapping) { // empty method-level RequestMapping urls.add(null); } } } }, ReflectionUtils.USER_DECLARED_METHODS);
String[] subclassResult = determineUrlsForHandlerMethods(handlerType); if (subclassResult != null) { return subclassResult;
private void findResources() { Map<String, Object> handlerMap = new HashMap<String, Object>(); DefaultAnnotationHandlerMapping annotationMapping = new DefaultAnnotationHandlerMapping(); annotationMapping.setApplicationContext(applicationContext); annotationMapping.initApplicationContext(); handlerMap.putAll(annotationMapping.getHandlerMap()); BeanNameUrlHandlerMapping beanMapping = new BeanNameUrlHandlerMapping(); beanMapping.setApplicationContext(applicationContext); beanMapping.initApplicationContext(); handlerMap.putAll(beanMapping.getHandlerMap()); this.urls = findUniqueUrls(handlerMap.keySet()); this.defaultResources = findMethods(handlerMap, this.urls); this.jsonResources = new ArrayList<ResourceInfo>(); for (Iterator<ResourceInfo> iterator = this.defaultResources.iterator(); iterator.hasNext();) { ResourceInfo info = (ResourceInfo) iterator.next(); if (info.getUrl().endsWith(".json")) { iterator.remove(); this.jsonResources.add(info); } } }