Search in sources :

Example 1 with DEFAULT_VIEW_INCLUSION

use of com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportTests method requestMappingHandlerAdapter.

@Test
public void requestMappingHandlerAdapter() {
    ApplicationContext context = initContext(WebConfig.class);
    RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
    List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
    assertThat(converters.size()).isEqualTo(12);
    converters.stream().filter(converter -> converter instanceof AbstractJackson2HttpMessageConverter).forEach(converter -> {
        ObjectMapper mapper = ((AbstractJackson2HttpMessageConverter) converter).getObjectMapper();
        assertThat(mapper.getDeserializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
        assertThat(mapper.getSerializationConfig().isEnabled(DEFAULT_VIEW_INCLUSION)).isFalse();
        assertThat(mapper.getDeserializationConfig().isEnabled(FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
        if (converter instanceof MappingJackson2XmlHttpMessageConverter) {
            assertThat(mapper.getClass()).isEqualTo(XmlMapper.class);
        }
    });
    ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) adapter.getWebBindingInitializer();
    assertThat(initializer).isNotNull();
    ConversionService conversionService = initializer.getConversionService();
    assertThat(conversionService).isNotNull();
    boolean condition1 = conversionService instanceof FormattingConversionService;
    assertThat(condition1).isTrue();
    Validator validator = initializer.getValidator();
    assertThat(validator).isNotNull();
    boolean condition = validator instanceof LocalValidatorFactoryBean;
    assertThat(condition).isTrue();
    DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(adapter);
    @SuppressWarnings("unchecked") List<Object> bodyAdvice = (List<Object>) fieldAccessor.getPropertyValue("requestResponseBodyAdvice");
    assertThat(bodyAdvice.size()).isEqualTo(2);
    assertThat(bodyAdvice.get(0).getClass()).isEqualTo(JsonViewRequestBodyAdvice.class);
    assertThat(bodyAdvice.get(1).getClass()).isEqualTo(JsonViewResponseBodyAdvice.class);
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PathVariable(org.springframework.web.bind.annotation.PathVariable) DefaultRequestToViewNameTranslator(org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator) DEFAULT_VIEW_INCLUSION(com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION) Validator(org.springframework.validation.Validator) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) LocaleResolver(org.springframework.web.servlet.LocaleResolver) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) 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) Map(java.util.Map) 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) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) HandlerMapping(org.springframework.web.servlet.HandlerMapping) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) FlashMapManager(org.springframework.web.servlet.FlashMapManager) 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) FLASH_MAP_MANAGER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) THEME_RESOLVER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER_BEAN_NAME) REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME) Ordered(org.springframework.core.Ordered) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ThemeResolver(org.springframework.web.servlet.ThemeResolver) 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) Scope(org.springframework.context.annotation.Scope) RequestToViewNameTranslator(org.springframework.web.servlet.RequestToViewNameTranslator) LOCALE_RESOLVER_BEAN_NAME(org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) 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) PathMatcher(org.springframework.util.PathMatcher) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode) 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) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Bean(org.springframework.context.annotation.Bean) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) 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.jupiter.api.Test)

Aggregations

FAIL_ON_UNKNOWN_PROPERTIES (com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)1 DEFAULT_VIEW_INCLUSION (com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 Date (java.util.Date)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ApplicationContext (org.springframework.context.ApplicationContext)1 MessageSource (org.springframework.context.MessageSource)1 Bean (org.springframework.context.annotation.Bean)1 Configuration (org.springframework.context.annotation.Configuration)1 Scope (org.springframework.context.annotation.Scope)1 ScopedProxyMode (org.springframework.context.annotation.ScopedProxyMode)1 LocaleContextHolder (org.springframework.context.i18n.LocaleContextHolder)1 StaticMessageSource (org.springframework.context.support.StaticMessageSource)1