Package org.springframework.web.servlet.mvc.method.annotation

Examples of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping


    private static final String RESOURCES_LOCATION = "/resources/";
    private static final String RESOURCES_HANDLER = RESOURCES_LOCATION + "**";

    @Override
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping();
        requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
        requestMappingHandlerMapping.setUseTrailingSlashMatch(false);
        return requestMappingHandlerMapping;
    }
View Full Code Here


   * Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
   * requests to annotated controllers.
   */
  @Bean
  public RequestMappingHandlerMapping requestMappingHandlerMapping() {
    RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
    handlerMapping.setOrder(0);
    handlerMapping.setInterceptors(getInterceptors());
    handlerMapping.setContentNegotiationManager(mvcContentNegotiationManager());

    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;
  }
View Full Code Here

  }

  @Test
  public void testPathMatchingConfiguration() {
    loadBeanDefinitions("mvc-config-path-matching.xml");
    RequestMappingHandlerMapping hm = 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));
  }
View Full Code Here

  @Test
  public void testDefaultConfig() throws Exception {
    loadBeanDefinitions("mvc-config.xml", 13);

    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertNotNull(mapping);
    assertEquals(0, mapping.getOrder());
    assertTrue(mapping.getUrlPathHelper().shouldRemoveSemicolonContent());
    mapping.setDefaultHandler(handlerMethod);

    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.json");
    NativeWebRequest webRequest = new ServletWebRequest(request);
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(webRequest));

    RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
    assertNotNull(adapter);
    assertEquals(false, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect"));

    List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
    assertTrue(converters.size() > 0);
    for(HttpMessageConverter<?> converter : converters) {
      if (converter instanceof AbstractJackson2HttpMessageConverter) {
        ObjectMapper objectMapper = ((AbstractJackson2HttpMessageConverter)converter).getObjectMapper();
        assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
        assertFalse(objectMapper.getSerializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
        assertFalse(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
        if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
          assertEquals(XmlMapper.class, objectMapper.getClass());
        }
      }
    }

    assertNotNull(appContext.getBean(FormattingConversionServiceFactoryBean.class));
    assertNotNull(appContext.getBean(ConversionService.class));
    assertNotNull(appContext.getBean(LocalValidatorFactoryBean.class));
    assertNotNull(appContext.getBean(Validator.class));

    // default web binding initializer behavior test
    request = new MockHttpServletRequest("GET", "/");
    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, handlerMethod);
    assertSame(appContext.getBean(ConversionService.class), request.getAttribute(ConversionService.class.getName()));
View Full Code Here

  @Test(expected=TypeMismatchException.class)
  public void testCustomConversionService() throws Exception {
    loadBeanDefinitions("mvc-config-custom-conversion-service.xml", 13);

    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()));
View Full Code Here

   * @throws Exception
   */
  private void doTestCustomValidator(String xml) throws Exception {
    loadBeanDefinitions(xml, 13);

    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"));

View Full Code Here

  @Test
  public void testInterceptors() throws Exception {
    loadBeanDefinitions("mvc-config-interceptors.xml", 20);

    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);
  }
View Full Code Here

  @Test
  public void testBeanDecoration() throws Exception {
    loadBeanDefinitions("mvc-config-bean-decoration.xml", 15);

    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];
View Full Code Here

  @Test
  public void testViewControllers() throws Exception {
    loadBeanDefinitions("mvc-config-view-controllers.xml", 18);

    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    assertNotNull(mapping);
    mapping.setDefaultHandler(handlerMethod);

    BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
    assertNotNull(beanNameMapping);
    assertEquals(2, beanNameMapping.getOrder());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("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);
View Full Code Here

  @Test
  public void testContentNegotiationManager() throws Exception {
    loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 13);

    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));
  }
View Full Code Here

TOP

Related Classes of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.