Search in sources :

Example 16 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class ContentNegotiationConfigurerTests method favorParameter.

@Test
public void favorParameter() throws Exception {
    this.configurer.favorParameter(true);
    this.configurer.parameterName("f");
    this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
    ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
    this.servletRequest.setRequestURI("/flower");
    this.servletRequest.addParameter("f", "json");
    assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) Test(org.junit.Test)

Example 17 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager 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 18 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewResolutionWithContentNegotiation.

@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
    loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml", 6);
    ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
    assertNotNull(compositeResolver);
    assertEquals(1, compositeResolver.getViewResolvers().size());
    assertEquals(Ordered.HIGHEST_PRECEDENCE, compositeResolver.getOrder());
    List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
    assertEquals(ContentNegotiatingViewResolver.class, resolvers.get(0).getClass());
    ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolvers.get(0);
    assertEquals(6, cnvr.getViewResolvers().size());
    assertEquals(1, cnvr.getDefaultViews().size());
    assertTrue(cnvr.isUseNotAcceptableStatusCode());
    String beanName = "contentNegotiationManager";
    DirectFieldAccessor accessor = new DirectFieldAccessor(cnvr);
    ContentNegotiationManager manager = (ContentNegotiationManager) accessor.getPropertyValue(beanName);
    assertNotNull(manager);
    assertSame(manager, this.appContext.getBean(ContentNegotiationManager.class));
    assertSame(manager, this.appContext.getBean("mvcContentNegotiationManager"));
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) ViewResolver(org.springframework.web.servlet.ViewResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) TilesViewResolver(org.springframework.web.servlet.view.tiles3.TilesViewResolver) Test(org.junit.Test)

Example 19 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method nestedViewResolverIsNotSpringBean.

@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
    StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();
    InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
    nestedResolver.setApplicationContext(webAppContext);
    nestedResolver.setViewClass(InternalResourceView.class);
    viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));
    FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
    viewResolver.afterPropertiesSet();
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    View result = viewResolver.resolveViewName(viewName, locale);
    assertNotNull("Invalid view", result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 20 with ContentNegotiationManager

use of org.springframework.web.accept.ContentNegotiationManager in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method resolveViewNameWithRequestParameter.

@Test
public void resolveViewNameWithRequestParameter() throws Exception {
    request.addParameter("format", "xls");
    Map<String, MediaType> mapping = Collections.singletonMap("xls", MediaType.valueOf("application/vnd.ms-excel"));
    ParameterContentNegotiationStrategy paramStrategy = new ParameterContentNegotiationStrategy(mapping);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(paramStrategy));
    ViewResolver viewResolverMock = mock(ViewResolver.class);
    viewResolver.setViewResolvers(Collections.singletonList(viewResolverMock));
    viewResolver.afterPropertiesSet();
    View viewMock = mock(View.class, "application_xls");
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    given(viewResolverMock.resolveViewName(viewName, locale)).willReturn(null);
    given(viewResolverMock.resolveViewName(viewName + ".xls", locale)).willReturn(viewMock);
    given(viewMock.getContentType()).willReturn("application/vnd.ms-excel");
    View result = viewResolver.resolveViewName(viewName, locale);
    assertSame("Invalid view", viewMock, result);
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MediaType(org.springframework.http.MediaType) ParameterContentNegotiationStrategy(org.springframework.web.accept.ParameterContentNegotiationStrategy) ViewResolver(org.springframework.web.servlet.ViewResolver) View(org.springframework.web.servlet.View) Test(org.junit.Test)

Aggregations

ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)24 Test (org.junit.Test)23 MediaType (org.springframework.http.MediaType)8 ViewResolver (org.springframework.web.servlet.ViewResolver)8 Locale (java.util.Locale)7 View (org.springframework.web.servlet.View)7 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 MockServletContext (org.springframework.mock.web.test.MockServletContext)4 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)4 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)4 ClassPathResource (org.springframework.core.io.ClassPathResource)3 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)3 PathExtensionContentNegotiationStrategy (org.springframework.web.accept.PathExtensionContentNegotiationStrategy)3 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)3 BeanNameViewResolver (org.springframework.web.servlet.view.BeanNameViewResolver)3 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Method (java.lang.reflect.Method)2