@Test(expected = TypeMismatchException.class) public void testCustomConversionService() throws Exception { loadBeanDefinitions("mvc-config-custom-conversion-service.xml", 14); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(mapping); mapping.setDefaultHandler(handlerMethod); // default web binding initializer behavior test MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); request.setRequestURI("/accounts/12345"); request.addParameter("date", "2009-10-31"); MockHttpServletResponse response = new MockHttpServletResponse(); HandlerExecutionChain chain = mapping.getHandler(request); assertEquals(1, chain.getInterceptors().length); assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor); ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0]; interceptor.preHandle(request, response, handler); assertSame(appContext.getBean("conversionService"), request.getAttribute(ConversionService.class.getName())); RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class); assertNotNull(adapter); adapter.handle(request, response, handlerMethod); }
@Override public void onApplicationEvent ( ContextRefreshedEvent event ) { final RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean( RequestMappingHandlerMapping.class ); final Map< RequestMappingInfo, HandlerMethod > handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); this.handlerMethods = handlerMethods; handlerMethods.keySet().forEach( mappingInfo -> { Map< Set< String >, Set< RequestMethod > > mapping = Collections.singletonMap( mappingInfo.getPatternsCondition().getPatterns() , this.getMethods( mappingInfo.getMethodsCondition().getMethods() ) ); requestMappingInfos.add( mapping ); } ); requestMappingUris.addAll( handlerMethods.keySet() .parallelStream() .map( mappingInfo -> mappingInfo.getPatternsCondition().getPatterns() ) .collect( Collectors.toList() ) ); }
public static List<OriginalRequestMappingInfo> of(RequestMappingHandlerMapping handlerMapping) { List<OriginalRequestMappingInfo> result = new ArrayList<OriginalRequestMappingInfo>(); for (Entry<RequestMappingInfo, HandlerMethod> entry : handlerMapping.getHandlerMethods().entrySet()) { RequestMappingInfo requestMappingInfo = entry.getKey(); OriginalRequestMappingInfo o = new OriginalRequestMappingInfo(); o.setSite(Site.of(requestMappingInfo.getPatternsCondition().getPatterns().iterator().next())); o.setMethods(requestMappingInfo.getMethodsCondition().getMethods()); o.setPatterns(requestMappingInfo.getPatternsCondition().getPatterns()); Set<String> params = new HashSet<>(); for (NameValueExpression<String> nameValueExpression : requestMappingInfo.getParamsCondition().getExpressions()) { params.add(nameValueExpression.toString()); } o.setParams(params); result.add(o); } return result.stream().sorted().collect(Collectors.toList()); }
private void doTestCustomValidator(String xml) throws Exception { loadBeanDefinitions(xml, 14); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(mapping); assertFalse(mapping.getUrlPathHelper().shouldRemoveSemicolonContent()); RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class); assertNotNull(adapter); assertEquals(true, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")); // default web binding initializer behavior test MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("date", "2009-10-31"); MockHttpServletResponse response = new MockHttpServletResponse(); adapter.handle(request, response, handlerMethod); assertTrue(appContext.getBean(TestValidator.class).validatorInvoked); assertFalse(handler.recordedValidationError); }
@Test public void testBeanDecoration() throws Exception { loadBeanDefinitions("mvc-config-bean-decoration.xml", 16); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(mapping); mapping.setDefaultHandler(handlerMethod); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); HandlerExecutionChain chain = mapping.getHandler(request); assertEquals(3, chain.getInterceptors().length); assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor); assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor); assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor); LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[1]; assertEquals("lang", interceptor.getParamName()); ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptors()[2]; assertEquals("style", interceptor2.getParamName()); }
@Test public void testContentNegotiationManager() throws Exception { loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 15); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); ContentNegotiationManager manager = mapping.getContentNegotiationManager(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml"); NativeWebRequest webRequest = new ServletWebRequest(request); assertEquals(Arrays.asList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest)); ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class); assertNotNull(compositeResolver); assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size()); ViewResolver resolver = compositeResolver.getViewResolvers().get(0); assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass()); ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver; assertSame(manager, cnvr.getContentNegotiationManager()); }
@Test public void testPathMatchingHandlerMappings() throws Exception { loadBeanDefinitions("mvc-config-path-matching-mappings.xml", 23); RequestMappingHandlerMapping requestMapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(requestMapping); assertEquals(TestPathHelper.class, requestMapping.getUrlPathHelper().getClass()); assertEquals(TestPathMatcher.class, requestMapping.getPathMatcher().getClass()); SimpleUrlHandlerMapping viewController = appContext.getBean(VIEWCONTROLLER_BEAN_NAME, SimpleUrlHandlerMapping.class); assertNotNull(viewController); assertEquals(TestPathHelper.class, viewController.getUrlPathHelper().getClass()); assertEquals(TestPathMatcher.class, viewController.getPathMatcher().getClass()); for (SimpleUrlHandlerMapping handlerMapping : appContext.getBeansOfType(SimpleUrlHandlerMapping.class).values()) { assertNotNull(handlerMapping); assertEquals(TestPathHelper.class, handlerMapping.getUrlPathHelper().getClass()); assertEquals(TestPathMatcher.class, handlerMapping.getPathMatcher().getClass()); } }
@Test public void requestMappingHandlerMapping() throws Exception { ApplicationContext context = initContext(WebConfig.class, ScopedController.class, ScopedProxyController.class); RequestMappingHandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class); assertEquals(0, handlerMapping.getOrder()); HandlerExecutionChain chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/")); assertNotNull(chain); assertNotNull(chain.getInterceptors()); assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[0].getClass()); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scoped")); assertNotNull("HandlerExecutionChain for '/scoped' mapping should not be null.", chain); chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/scopedProxy")); assertNotNull("HandlerExecutionChain for '/scopedProxy' mapping should not be null.", chain); }
@Test public void suffixPatternMatch() throws Exception { TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PersonController()); builder.setUseSuffixPatternMatch(false); builder.build(); RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/persons"); HandlerExecutionChain chain = hm.getHandler(request); assertNotNull(chain); assertEquals("persons", ((HandlerMethod) chain.getHandler()).getMethod().getName()); request = new MockHttpServletRequest("GET", "/persons.xml"); chain = hm.getHandler(request); assertNull(chain); }
@Test public void sitePreferenceHandlerInterceptorRegistered() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, SitePreferenceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); RequestMappingHandlerMapping mapping = this.context .getBean(RequestMappingHandlerMapping.class); HandlerInterceptor[] interceptors = mapping .getHandler(new MockHttpServletRequest()).getInterceptors(); assertThat(interceptors) .hasAtLeastOneElementOfType(SitePreferenceHandlerInterceptor.class); }
@Bean public WebMvcRegistrations webMvcRegistrations() { return new WebMvcRegistrationsAdapter() { @Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { DefaultRequestMappingHandlerMapping mapping = new DefaultRequestMappingHandlerMapping(); mapping.setControllerPostfix("Controller"); mapping.setExcludePatterns(new String[]{ "/js/**", "/css/**", "/imgs/**" }); return mapping; } @Override public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() { DefaultRequestMappingHandlerAdapter adapter = new DefaultRequestMappingHandlerAdapter(); adapter.setAutoView(true); return adapter; } }; }
protected HandlerExecutionChain getHandlerExecution(HttpServletRequest request) throws Exception { WebApplicationContext appContext = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getServletContext()); HandlerMapping bean = appContext.getBean(RequestMappingHandlerMapping.class); HandlerExecutionChain handler = bean.getHandler(request); if (handler == null) { ServletContext servletContext = request.getServletContext(); Enumeration<?> attrNameEnum = servletContext.getAttributeNames(); while (attrNameEnum.hasMoreElements()) { String attrName = (String) attrNameEnum.nextElement(); if (attrName.startsWith(FrameworkServlet.SERVLET_CONTEXT_PREFIX)) { appContext = (WebApplicationContext) servletContext.getAttribute(attrName); bean = appContext.getBean(RequestMappingHandlerMapping.class); handler = bean.getHandler(request); if (handler != null) { break; } } } } return handler; }
@Test public void sitePreferenceHandlerInterceptorRegistered() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, SitePreferenceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); RequestMappingHandlerMapping mapping = this.context .getBean(RequestMappingHandlerMapping.class); HandlerInterceptor[] interceptors = mapping .getHandler(new MockHttpServletRequest()).getInterceptors(); assertThat(interceptors, hasItemInArray(instanceOf(SitePreferenceHandlerInterceptor.class))); }
@Test public void deviceResolverHandlerInterceptorRegistered() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, DeviceResolverAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); RequestMappingHandlerMapping mapping = this.context .getBean(RequestMappingHandlerMapping.class); HandlerInterceptor[] interceptors = mapping .getHandler(new MockHttpServletRequest()).getInterceptors(); assertThat(interceptors, hasItemInArray(instanceOf(DeviceResolverHandlerInterceptor.class))); }
/** * 获得 request mapping handler mapping info map for LOGGER. * * @param webApplicationContext * the web application context * @return the request mapping handler mapping info map for log */ public static final Map<String, Object> getRequestMappingHandlerMappingInfoMapForLog(WebApplicationContext webApplicationContext){ RequestMappingHandlerMapping requestMappingHandlerMapping = webApplicationContext.getBean(RequestMappingHandlerMapping.class); Map<String, Object> mappingInfoMap = newLinkedHashMap(); mappingInfoMap.put("useRegisteredSuffixPatternMatch()", requestMappingHandlerMapping.useRegisteredSuffixPatternMatch()); mappingInfoMap.put("useSuffixPatternMatch()", requestMappingHandlerMapping.useSuffixPatternMatch()); mappingInfoMap.put("useTrailingSlashMatch()", requestMappingHandlerMapping.useTrailingSlashMatch()); mappingInfoMap.put("getDefaultHandler()", requestMappingHandlerMapping.getDefaultHandler()); mappingInfoMap.put("getFileExtensions()", requestMappingHandlerMapping.getFileExtensions()); mappingInfoMap.put("getOrder()", requestMappingHandlerMapping.getOrder()); mappingInfoMap.put("getPathMatcher()", requestMappingHandlerMapping.getPathMatcher()); mappingInfoMap.put("getUrlPathHelper()", requestMappingHandlerMapping.getUrlPathHelper()); //--------------------------------------------------------------- Map<String, RequestMappingInfo> methodAndRequestMappingInfoMapMap = buildMethodAndRequestMappingInfoMap( requestMappingHandlerMapping); mappingInfoMap.put("methodAndRequestMappingInfoMapMap", methodAndRequestMappingInfoMapMap); return mappingInfoMap; }
/** * key 是handle 方法,value 是 RequestMappingInfo 信息. * * @param requestMappingHandlerMapping * the request mapping handler mapping * @return the map< string, request mapping info> * @see org.springframework.web.servlet.mvc.method.RequestMappingInfo * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#createRequestMappingInfo(RequestMapping, * RequestCondition) * @since 1.5.4 */ private static Map<String, RequestMappingInfo> buildMethodAndRequestMappingInfoMap( RequestMappingHandlerMapping requestMappingHandlerMapping){ Map<String, RequestMappingInfo> methodAndRequestMappingInfoMap = newLinkedHashMap(); //--------------------------------------------------------------- Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()){ RequestMappingInfo requestMappingInfo = entry.getKey(); HandlerMethod handlerMethod = entry.getValue(); methodAndRequestMappingInfoMap.put(handlerMethod.toString(), requestMappingInfo); } if (LOGGER.isInfoEnabled()){ Collection<RequestMappingInfo> requestMappingInfoCollection = methodAndRequestMappingInfoMap.values(); String format = JsonUtil.format(getPropertyValueList(requestMappingInfoCollection, "patternsCondition.patterns")); LOGGER.info("all requestMapping value:{}", format); } return methodAndRequestMappingInfoMap; }
@Bean public WebMvcRegistrationsAdapter webMvcRegistrationsAdapter() { return new WebMvcRegistrationsAdapter() { @Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { return new RequestMappingHandlerMapping() { @Override protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) { if (method.getDeclaringClass().isAnnotationPresent(SpringRestClientEnabled.class)) { return; // by pass SpringRestClientEnabled interface } super.registerHandlerMethod(handler, method, mapping); } }; } }; }
private void registerMvcSingletons(StubWebApplicationContext cxt) { StandaloneConfiguration configuration = new StandaloneConfiguration(); RequestMappingHandlerMapping handlerMapping = configuration.requestMappingHandlerMapping(); handlerMapping.setServletContext(cxt.getServletContext()); handlerMapping.setApplicationContext(cxt); cxt.addBean("requestMappingHandlerMapping", handlerMapping); RequestMappingHandlerAdapter handlerAdapter = configuration.requestMappingHandlerAdapter(); handlerAdapter.setServletContext(cxt.getServletContext()); handlerAdapter.setApplicationContext(cxt); handlerAdapter.afterPropertiesSet(); cxt.addBean("requestMappingHandlerAdapter", handlerAdapter); cxt.addBean("handlerExceptionResolver", configuration.handlerExceptionResolver()); cxt.addBeans(initViewResolvers(cxt)); cxt.addBean(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, this.localeResolver); cxt.addBean(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, new FixedThemeResolver()); cxt.addBean(DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME, new DefaultRequestToViewNameTranslator()); this.flashMapManager = new SessionFlashMapManager(); cxt.addBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, this.flashMapManager); }
@Test(expected=TypeMismatchException.class) public void testCustomConversionService() throws Exception { loadBeanDefinitions("mvc-config-custom-conversion-service.xml", 12); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(mapping); mapping.setDefaultHandler(handlerMethod); // default web binding initializer behavior test MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); request.setRequestURI("/accounts/12345"); request.addParameter("date", "2009-10-31"); MockHttpServletResponse response = new MockHttpServletResponse(); HandlerExecutionChain chain = mapping.getHandler(request); assertEquals(1, chain.getInterceptors().length); assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor); ConversionServiceExposingInterceptor interceptor = (ConversionServiceExposingInterceptor) chain.getInterceptors()[0]; interceptor.preHandle(request, response, handler); assertSame(appContext.getBean("conversionService"), request.getAttribute(ConversionService.class.getName())); RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class); assertNotNull(adapter); adapter.handle(request, response, handlerMethod); }
@Test public void testCustomValidator() throws Exception { loadBeanDefinitions("mvc-config-custom-validator.xml", 12); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(mapping); assertFalse(mapping.getUrlPathHelper().shouldRemoveSemicolonContent()); RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class); assertNotNull(adapter); assertEquals(true, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")); // default web binding initializer behavior test MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("date", "2009-10-31"); MockHttpServletResponse response = new MockHttpServletResponse(); adapter.handle(request, response, handlerMethod); assertTrue(appContext.getBean(TestValidator.class).validatorInvoked); assertFalse(handler.recordedValidationError); }
@Test public void testBeanDecoration() throws Exception { loadBeanDefinitions("mvc-config-bean-decoration.xml", 14); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(mapping); mapping.setDefaultHandler(handlerMethod); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); HandlerExecutionChain chain = mapping.getHandler(request); assertEquals(3, chain.getInterceptors().length); assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor); assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor); assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor); LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[1]; assertEquals("lang", interceptor.getParamName()); ThemeChangeInterceptor interceptor2 = (ThemeChangeInterceptor) chain.getInterceptors()[2]; assertEquals("style", interceptor2.getParamName()); }
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof RequestMappingHandlerMapping) { ((RequestMappingHandlerMapping) bean).setUseSuffixPatternMatch(false); } return bean; }
private RequestMappingHandlerMapping requestMappingHandlerMapping() { try { return ctx.getBean(RequestMappingHandlerMapping.class); } catch (Exception e) { throw new IllegalArgumentException("applicationContext must has RequestMappingHandlerMapping"); } }
private void setupHomeExtension(ModuleContext context) { if (ClassUtils.existsClass("io.crnk.home.HomeModuleExtension")) { try { Class clazz = Class.forName("io.crnk.spring.mvc.internal.SpringMvcHomeModuleExtensionFactory"); Method method = clazz.getMethod("create", RequestMappingHandlerMapping.class); ModuleExtension homeExtension = (ModuleExtension) method.invoke(clazz, handlerMapping); context.addExtension(homeExtension); } catch (Exception e) { throw new IllegalStateException(e); } } }
public static HomeModuleExtension create(RequestMappingHandlerMapping mapping) { HomeModuleExtension ext = new HomeModuleExtension(); Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods(); for (RequestMappingInfo info : handlerMethods.keySet()) { Set<String> patterns = info.getPatternsCondition().getPatterns(); for (String pattern : patterns) { ext.addPath(pattern); } } return ext; }
@Override protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() { RequestMappingHandlerMapping handlerMapping = super.createRequestMappingHandlerMapping(); Object defaultHandler = createDefaultHandler(); handlerMapping.setDefaultHandler(defaultHandler); return handlerMapping; }
@Bean public WebMvcRegistrations feignWebRegistrations() { return new WebMvcRegistrationsAdapter() { @Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { return new FeignRequestMappingHandlerMapping(); } }; }
/** * We mention this in the book, but this helps to ensure that the intercept-url patterns prevent access to our * controllers. For example, once security has been applied for administrators try commenting out the modifications * to the super class and requesting <a * href="http://localhost:800/calendar/events/.html">http://localhost:800/calendar/events/.html</a>. You will * observe that security is bypassed since it did not match the pattern we provided. In later chapters, we discuss * how to secure the service tier which helps mitigate bypassing of the URL based security too. */ // FIXME: FInd out what this is and why it is here. @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping result = new RequestMappingHandlerMapping(); result.setUseSuffixPatternMatch(false); result.setUseTrailingSlashMatch(false); return result; }
@Bean public WebMvcRegistrations feignWebRegistrations() { return new WebMvcRegistrationsAdapter() { @Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping() { return new RequestMappingHandlerMapping() { @Override protected boolean isHandler(Class<?> beanType) { return super.isHandler(beanType) && (AnnotationUtils.findAnnotation(beanType, FeignClient.class) == null); } }; } }; }
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof RequestMappingHandlerMapping) { RequestMappingHandlerMapping requestMappingHandlerMapping = ((RequestMappingHandlerMapping) bean); logger.info("Setting 'RemoveSemicolonContent' on 'RequestMappingHandlerMapping'-bean to false. Bean name: {}", beanName); requestMappingHandlerMapping.setRemoveSemicolonContent(false); logger.info("Setting 'UseSuffixPatternMatch' on 'RequestMappingHandlerMapping'-bean to false. Bean name: {}", beanName); requestMappingHandlerMapping.setUseSuffixPatternMatch(false); } return bean; }
/** * Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping * requests to annotated controllers. */ @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping handlerMapping = createRequestMappingHandlerMapping(); handlerMapping.setOrder(0); handlerMapping.setInterceptors(getInterceptors()); handlerMapping.setContentNegotiationManager(mvcContentNegotiationManager()); handlerMapping.setCorsConfigurations(getCorsConfigurations()); PathMatchConfigurer configurer = getPathMatchConfigurer(); if (configurer.isUseSuffixPatternMatch() != null) { handlerMapping.setUseSuffixPatternMatch(configurer.isUseSuffixPatternMatch()); } if (configurer.isUseRegisteredSuffixPatternMatch() != null) { handlerMapping.setUseRegisteredSuffixPatternMatch(configurer.isUseRegisteredSuffixPatternMatch()); } if (configurer.isUseTrailingSlashMatch() != null) { handlerMapping.setUseTrailingSlashMatch(configurer.isUseTrailingSlashMatch()); } if (configurer.getPathMatcher() != null) { handlerMapping.setPathMatcher(configurer.getPathMatcher()); } if (configurer.getUrlPathHelper() != null) { handlerMapping.setUrlPathHelper(configurer.getUrlPathHelper()); } return handlerMapping; }
@Test public void testInterceptors() throws Exception { loadBeanDefinitions("mvc-config-interceptors.xml", 21); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(mapping); mapping.setDefaultHandler(handlerMethod); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); request.setRequestURI("/accounts/12345"); request.addParameter("locale", "en"); request.addParameter("theme", "green"); HandlerExecutionChain chain = mapping.getHandler(request); assertEquals(5, chain.getInterceptors().length); assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor); assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor); assertTrue(chain.getInterceptors()[2] instanceof WebRequestHandlerInterceptorAdapter); assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor); assertTrue(chain.getInterceptors()[4] instanceof UserRoleAuthorizationInterceptor); request.setRequestURI("/admin/users"); chain = mapping.getHandler(request); assertEquals(3, chain.getInterceptors().length); request.setRequestURI("/logged/accounts/12345"); chain = mapping.getHandler(request); assertEquals(5, chain.getInterceptors().length); assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter); request.setRequestURI("/foo/logged"); chain = mapping.getHandler(request); assertEquals(5, chain.getInterceptors().length); assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter); }