@Override public void afterPropertiesSet() { this.config = new RequestMappingInfo.BuilderConfiguration(); this.config.setUrlPathHelper(getUrlPathHelper()); this.config.setPathMatcher(getPathMatcher()); this.config.setSuffixPatternMatch(this.useSuffixPatternMatch); this.config.setTrailingSlashMatch(this.useTrailingSlashMatch); this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch); this.config.setContentNegotiationManager(getContentNegotiationManager()); super.afterPropertiesSet(); }
@Test public void testPathMatchingConfiguration() { loadBeanDefinitions("mvc-config-path-matching.xml"); RequestMappingHandlerMapping hm = this.appContext.getBean(RequestMappingHandlerMapping.class); assertNotNull(hm); assertTrue(hm.useSuffixPatternMatch()); assertFalse(hm.useTrailingSlashMatch()); assertTrue(hm.useRegisteredSuffixPatternMatch()); assertThat(hm.getUrlPathHelper(), Matchers.instanceOf(TestPathHelper.class)); assertThat(hm.getPathMatcher(), Matchers.instanceOf(TestPathMatcher.class)); List<String> fileExtensions = hm.getContentNegotiationManager().getAllFileExtensions(); assertThat(fileExtensions, Matchers.contains("xml")); assertThat(fileExtensions, Matchers.hasSize(1)); }
@Test public void testContentNegotiationManager() throws Exception { loadBeanDefinitions("mvc-config-content-negotiation-manager.xml"); RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class); ContentNegotiationManager manager = mapping.getContentNegotiationManager(); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml"); NativeWebRequest webRequest = new ServletWebRequest(request); assertEquals(Collections.singletonList(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()); }
@Override public void afterPropertiesSet() { this.config = new RequestMappingInfo.BuilderConfiguration(); this.config.setUrlPathHelper(getUrlPathHelper()); this.config.setPathMatcher(getPathMatcher()); this.config.setSuffixPatternMatch(this.useSuffixPatternMatch); this.config.setTrailingSlashMatch(this.useTrailingSlashMatch); this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch); this.config.setContentNegotiationManager(getContentNegotiationManager()); super.afterPropertiesSet(); }
@Test public void contentNegotiation() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json"); NativeWebRequest webRequest = new ServletWebRequest(request); RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping(); ContentNegotiationManager manager = mapping.getContentNegotiationManager(); assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest)); request.setRequestURI("/foo.xml"); assertEquals(Collections.singletonList(APPLICATION_XML), manager.resolveMediaTypes(webRequest)); request.setRequestURI("/foo.rss"); assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest)); request.setRequestURI("/foo.atom"); assertEquals(Collections.singletonList(APPLICATION_ATOM_XML), manager.resolveMediaTypes(webRequest)); request.setRequestURI("/foo"); request.setParameter("f", "json"); assertEquals(Collections.singletonList(APPLICATION_JSON), manager.resolveMediaTypes(webRequest)); request.setRequestURI("/resources/foo.gif"); SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping(); handlerMapping.setApplicationContext(this.context); HandlerExecutionChain chain = handlerMapping.getHandler(request); assertNotNull(chain); ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler(); assertNotNull(handler); assertSame(manager, handler.getContentNegotiationManager()); }
ContentNegotiationManager manager = mapping.getContentNegotiationManager();
ContentNegotiationManager manager = mapping.getContentNegotiationManager(); assertEquals(Collections.singletonList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(webRequest));
@Override public void afterPropertiesSet() { this.config = new RequestMappingInfo.BuilderConfiguration(); this.config.setUrlPathHelper(getUrlPathHelper()); this.config.setPathMatcher(getPathMatcher()); this.config.setSuffixPatternMatch(this.useSuffixPatternMatch); this.config.setTrailingSlashMatch(this.useTrailingSlashMatch); this.config.setRegisteredSuffixPatternMatch(this.useRegisteredSuffixPatternMatch); this.config.setContentNegotiationManager(getContentNegotiationManager()); super.afterPropertiesSet(); }