Search in sources :

Example 16 with RequestMappingHandlerAdapter

use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportExtensionTests method requestMappingHandlerAdapter.

@SuppressWarnings("unchecked")
@Test
public void requestMappingHandlerAdapter() throws Exception {
    RequestMappingHandlerAdapter adapter = this.config.requestMappingHandlerAdapter();
    // ConversionService
    String actual = this.config.mvcConversionService().convert(new TestBean(), String.class);
    assertEquals("converted", actual);
    // Message converters
    List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
    assertEquals(2, converters.size());
    assertEquals(StringHttpMessageConverter.class, converters.get(0).getClass());
    assertEquals(MappingJackson2HttpMessageConverter.class, converters.get(1).getClass());
    ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter) converters.get(1)).getObjectMapper();
    assertFalse(objectMapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
    assertFalse(objectMapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
    assertFalse(objectMapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
    DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
    // Custom argument resolvers and return value handlers
    List<HandlerMethodArgumentResolver> argResolvers = (List<HandlerMethodArgumentResolver>) fieldAccessor.getPropertyValue("customArgumentResolvers");
    assertEquals(1, argResolvers.size());
    List<HandlerMethodReturnValueHandler> handlers = (List<HandlerMethodReturnValueHandler>) fieldAccessor.getPropertyValue("customReturnValueHandlers");
    assertEquals(1, handlers.size());
    // Async support options
    assertEquals(ConcurrentTaskExecutor.class, fieldAccessor.getPropertyValue("taskExecutor").getClass());
    assertEquals(2500L, fieldAccessor.getPropertyValue("asyncRequestTimeout"));
    CallableProcessingInterceptor[] callableInterceptors = (CallableProcessingInterceptor[]) fieldAccessor.getPropertyValue("callableInterceptors");
    assertEquals(1, callableInterceptors.length);
    DeferredResultProcessingInterceptor[] deferredResultInterceptors = (DeferredResultProcessingInterceptor[]) fieldAccessor.getPropertyValue("deferredResultInterceptors");
    assertEquals(1, deferredResultInterceptors.length);
    assertEquals(false, fieldAccessor.getPropertyValue("ignoreDefaultModelOnRedirect"));
}
Also used : HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) DeferredResultProcessingInterceptor(org.springframework.web.context.request.async.DeferredResultProcessingInterceptor) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) TestBean(org.springframework.tests.sample.beans.TestBean) CallableProcessingInterceptor(org.springframework.web.context.request.async.CallableProcessingInterceptor) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) List(java.util.List) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 17 with RequestMappingHandlerAdapter

use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportExtensionTests method webBindingInitializer.

@Test
public void webBindingInitializer() throws Exception {
    RequestMappingHandlerAdapter adapter = this.config.requestMappingHandlerAdapter();
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    assertNotNull(initializer);
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(null, "");
    initializer.getValidator().validate(null, bindingResult);
    assertEquals("invalid", bindingResult.getAllErrors().get(0).getCode());
    String[] codes = initializer.getMessageCodesResolver().resolveMessageCodes("invalid", null);
    assertEquals("custom.invalid", codes[0]);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Example 18 with RequestMappingHandlerAdapter

use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter in project spring-framework by spring-projects.

the class MvcNamespaceTests method testDefaultConfig.

@Test
public void testDefaultConfig() throws Exception {
    loadBeanDefinitions("mvc-config.xml", 14);
    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(Collections.singletonList(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");
    request.addParameter("percent", "99.99%");
    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()));
    adapter.handle(request, response, handlerMethod);
    assertTrue(handler.recordedValidationError);
    assertEquals(LocalDate.parse("2009-10-31").toDate(), handler.date);
    assertEquals(Double.valueOf(0.9999), handler.percent);
    CompositeUriComponentsContributor uriComponentsContributor = this.appContext.getBean(MvcUriComponentsBuilder.MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME, CompositeUriComponentsContributor.class);
    assertNotNull(uriComponentsContributor);
}
Also used : LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) ConversionService(org.springframework.core.convert.ConversionService) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validator(org.springframework.validation.Validator) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 19 with RequestMappingHandlerAdapter

use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter in project spring-boot by spring-projects.

the class HypermediaAutoConfigurationTests method supportedMediaTypesOfTypeConstrainedConvertersIsCustomized.

@Test
public void supportedMediaTypesOfTypeConstrainedConvertersIsCustomized() {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(BaseConfig.class);
    this.context.refresh();
    RequestMappingHandlerAdapter handlerAdapter = this.context.getBean(RequestMappingHandlerAdapter.class);
    for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
        if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
            assertThat(converter.getSupportedMediaTypes()).contains(MediaType.APPLICATION_JSON, MediaTypes.HAL_JSON);
        }
    }
}
Also used : TypeConstrainedMappingJackson2HttpMessageConverter(org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 20 with RequestMappingHandlerAdapter

use of org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter in project spring-boot by spring-projects.

the class WebMvcAutoConfigurationTests method customMediaTypes.

@Test
public void customMediaTypes() throws Exception {
    load("spring.mvc.mediaTypes.yaml:text/yaml");
    RequestMappingHandlerAdapter adapter = this.context.getBean(RequestMappingHandlerAdapter.class);
    ContentNegotiationManager actual = (ContentNegotiationManager) ReflectionTestUtils.getField(adapter, "contentNegotiationManager");
    assertThat(actual.getAllFileExtensions().contains("yaml")).isTrue();
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Aggregations

RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)21 Test (org.junit.Test)17 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)5 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)4 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ConversionService (org.springframework.core.convert.ConversionService)3 LocalValidatorFactoryBean (org.springframework.validation.beanvalidation.LocalValidatorFactoryBean)3 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)3 ConversionServiceExposingInterceptor (org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor)3 List (java.util.List)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Bean (org.springframework.context.annotation.Bean)2 FormattingConversionService (org.springframework.format.support.FormattingConversionService)2 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)2 HandlerExceptionResolverComposite (org.springframework.web.servlet.handler.HandlerExceptionResolverComposite)2