Search in sources :

Example 6 with RequestMappingHandlerAdapter

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

the class WebMvcAutoConfigurationTests method defaultAsyncRequestTimeout.

@Test
public void defaultAsyncRequestTimeout() throws Exception {
    load();
    RequestMappingHandlerAdapter adapter = this.context.getBean(RequestMappingHandlerAdapter.class);
    assertThat(ReflectionTestUtils.getField(adapter, "asyncRequestTimeout")).isNull();
}
Also used : RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.Test)

Example 7 with RequestMappingHandlerAdapter

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

the class WebMvcConfigurationSupport method requestMappingHandlerAdapter.

/**
	 * Returns a {@link RequestMappingHandlerAdapter} for processing requests
	 * through annotated controller methods. Consider overriding one of these
	 * other more fine-grained methods:
	 * <ul>
	 * <li>{@link #addArgumentResolvers} for adding custom argument resolvers.
	 * <li>{@link #addReturnValueHandlers} for adding custom return value handlers.
	 * <li>{@link #configureMessageConverters} for adding custom message converters.
	 * </ul>
	 */
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
    RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
    adapter.setContentNegotiationManager(mvcContentNegotiationManager());
    adapter.setMessageConverters(getMessageConverters());
    adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
    adapter.setCustomArgumentResolvers(getArgumentResolvers());
    adapter.setCustomReturnValueHandlers(getReturnValueHandlers());
    if (jackson2Present) {
        adapter.setRequestBodyAdvice(Collections.singletonList(new JsonViewRequestBodyAdvice()));
        adapter.setResponseBodyAdvice(Collections.singletonList(new JsonViewResponseBodyAdvice()));
    }
    AsyncSupportConfigurer configurer = new AsyncSupportConfigurer();
    configureAsyncSupport(configurer);
    if (configurer.getTaskExecutor() != null) {
        adapter.setTaskExecutor(configurer.getTaskExecutor());
    }
    if (configurer.getTimeout() != null) {
        adapter.setAsyncRequestTimeout(configurer.getTimeout());
    }
    adapter.setCallableInterceptors(configurer.getCallableInterceptors());
    adapter.setDeferredResultInterceptors(configurer.getDeferredResultInterceptors());
    return adapter;
}
Also used : JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Bean(org.springframework.context.annotation.Bean)

Example 8 with RequestMappingHandlerAdapter

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

the class WebMvcConfigurationSupportTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() throws Exception {
    ApplicationContext context = initContext(WebConfig.class);
    RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
    List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
    assertEquals(11, converters.size());
    converters.stream().filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter).forEach(converter -> {
        ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
        assertFalse(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
        assertFalse(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION));
        assertFalse(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES));
        if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
            assertEquals(XmlMapper.class, mapper.getClass());
        }
    });
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    assertNotNull(initializer);
    ConversionService conversionService = initializer.getConversionService();
    assertNotNull(conversionService);
    assertTrue(conversionService instanceof FormattingConversionService);
    Validator validator = initializer.getValidator();
    assertNotNull(validator);
    assertTrue(validator instanceof LocalValidatorFactoryBean);
    DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
    @SuppressWarnings("unchecked") List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
    assertEquals(2, bodyAdvice.size());
    assertEquals(JsonViewRequestBodyAdvice.class, bodyAdvice.get(0).getClass());
    assertEquals(JsonViewResponseBodyAdvice.class, bodyAdvice.get(1).getClass());
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PathVariable(org.springframework.web.bind.annotation.PathVariable) DEFAULT_VIEW_INCLUSION(com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION) Validator(org.springframework.validation.Validator) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) FAIL_ON_UNKNOWN_PROPERTIES(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Locale(java.util.Locale) MethodParameter(org.springframework.core.MethodParameter) AntPathMatcher(org.springframework.util.AntPathMatcher) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) JsonViewRequestBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Configuration(org.springframework.context.annotation.Configuration) MockServletContext(org.springframework.mock.web.test.MockServletContext) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Assert.assertFalse(org.junit.Assert.assertFalse) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) Ordered(org.springframework.core.Ordered) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JsonViewResponseBodyAdvice(org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Controller(org.springframework.stereotype.Controller) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Scope(org.springframework.context.annotation.Scope) HttpServletRequest(javax.servlet.http.HttpServletRequest) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ConversionService(org.springframework.core.convert.ConversionService) MessageSource(org.springframework.context.MessageSource) ViewResolver(org.springframework.web.servlet.ViewResolver) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) PathMatcher(org.springframework.util.PathMatcher) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) Assert.assertNotNull(org.junit.Assert.assertNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) ScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FormattingConversionService(org.springframework.format.support.FormattingConversionService) ApplicationContext(org.springframework.context.ApplicationContext) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) HttpStatus(org.springframework.http.HttpStatus) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) Assert.assertNull(org.junit.Assert.assertNull) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Bean(org.springframework.context.annotation.Bean) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) Assert.assertEquals(org.junit.Assert.assertEquals) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) FormattingConversionService(org.springframework.format.support.FormattingConversionService) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) ConversionService(org.springframework.core.convert.ConversionService) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) List(java.util.List) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Validator(org.springframework.validation.Validator) Test(org.junit.Test)

Example 9 with RequestMappingHandlerAdapter

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

the class MvcNamespaceTests method testCustomConversionService.

@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);
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with RequestMappingHandlerAdapter

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

the class DelegatingWebMvcConfigurationTests method configureMessageConverters.

@Test
public void configureMessageConverters() {
    final HttpMessageConverter customConverter = mock(HttpMessageConverter.class);
    final StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
    List<WebMvcConfigurer> configurers = new ArrayList<>();
    configurers.add(new WebMvcConfigurerAdapter() {

        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(stringConverter);
        }

        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(0, customConverter);
        }
    });
    delegatingConfig = new DelegatingWebMvcConfiguration();
    delegatingConfig.setConfigurers(configurers);
    RequestMappingHandlerAdapter adapter = delegatingConfig.requestMappingHandlerAdapter();
    assertEquals("Only one custom converter should be registered", 2, adapter.getMessageConverters().size());
    assertSame(customConverter, adapter.getMessageConverters().get(0));
    assertSame(stringConverter, adapter.getMessageConverters().get(1));
}
Also used : StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ArrayList(java.util.ArrayList) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) 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