Search in sources :

Example 6 with ContentNegotiationManagerFactoryBean

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

the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.setFavorPathExtension(false);
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(new MockServletContext());
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.addHeader("Accept", "application/json,text/plain,*/*");
    this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
    handler.handleRequest(this.request, this.response);
    assertEquals("text/html", this.response.getContentType());
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 7 with ContentNegotiationManagerFactoryBean

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

the class ResourceHttpRequestHandlerTests method getResourceWithRegisteredMediaType.

// SPR-13658
@Test
public void getResourceWithRegisteredMediaType() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.addMediaType("css", new MediaType("foo", "bar"));
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(new MockServletContext());
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
    handler.handleRequest(this.request, this.response);
    assertEquals("foo/bar", this.response.getContentType());
    assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 8 with ContentNegotiationManagerFactoryBean

use of org.springframework.web.accept.ContentNegotiationManagerFactoryBean in project spring-security-oauth by spring-projects.

the class WebMvcConfig method contentViewResolver.

@Bean
public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
    ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
    contentViewResolver.setDefaultViews(Arrays.<View>asList(new MappingJackson2JsonView()));
    return contentViewResolver;
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) Bean(org.springframework.context.annotation.Bean) ConversionServiceFactoryBean(org.springframework.context.support.ConversionServiceFactoryBean)

Example 9 with ContentNegotiationManagerFactoryBean

use of org.springframework.web.accept.ContentNegotiationManagerFactoryBean in project spring-security-oauth by spring-projects.

the class WebMvcConfig method contentViewResolver.

@Bean
public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
    ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
    defaultView.setExtractValueFromSingleKeyModel(true);
    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
    contentViewResolver.setViewResolvers(Arrays.<ViewResolver>asList(viewResolver));
    contentViewResolver.setDefaultViews(Arrays.<View>asList(defaultView));
    return contentViewResolver;
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

ContentNegotiationManagerFactoryBean (org.springframework.web.accept.ContentNegotiationManagerFactoryBean)9 Test (org.junit.Test)7 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)4 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)4 Bean (org.springframework.context.annotation.Bean)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2 UrlResource (org.springframework.core.io.UrlResource)2 MediaType (org.springframework.http.MediaType)2 MockServletContext (org.springframework.mock.web.test.MockServletContext)2 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)2 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)2 MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)2 ConversionServiceFactoryBean (org.springframework.context.support.ConversionServiceFactoryBean)1 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)1 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)1