Search in sources :

Example 1 with ContentNegotiatingViewResolver

use of org.springframework.web.servlet.view.ContentNegotiatingViewResolver in project spring-framework by spring-projects.

the class ViewResolutionTests method testContentNegotiation.

@Test
public void testContentNegotiation() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Person.class);
    List<View> viewList = new ArrayList<>();
    viewList.add(new MappingJackson2JsonView());
    viewList.add(new MarshallingView(marshaller));
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
    ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
    cnViewResolver.setDefaultViews(viewList);
    cnViewResolver.setContentNegotiationManager(manager);
    cnViewResolver.afterPropertiesSet();
    MockMvc mockMvc = standaloneSetup(new PersonController()).setViewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
    mockMvc.perform(get("/person/Corea")).andExpect(status().isOk()).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.person.name").value("Corea"));
    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_XML)).andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
Also used : ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) View(org.springframework.web.servlet.View) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) MockMvc(org.springframework.test.web.servlet.MockMvc) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.Test)

Example 2 with ContentNegotiatingViewResolver

use of org.springframework.web.servlet.view.ContentNegotiatingViewResolver in project spring-framework by spring-projects.

the class ViewResolverRegistry method initContentNegotiatingViewResolver.

private void initContentNegotiatingViewResolver(View[] defaultViews) {
    // ContentNegotiatingResolver in the registry: elevate its precedence!
    this.order = (this.order != null ? this.order : Ordered.HIGHEST_PRECEDENCE);
    if (this.contentNegotiatingResolver != null) {
        if (!ObjectUtils.isEmpty(defaultViews)) {
            if (!CollectionUtils.isEmpty(this.contentNegotiatingResolver.getDefaultViews())) {
                List<View> views = new ArrayList<>(this.contentNegotiatingResolver.getDefaultViews());
                views.addAll(Arrays.asList(defaultViews));
                this.contentNegotiatingResolver.setDefaultViews(views);
            }
        }
    } else {
        this.contentNegotiatingResolver = new ContentNegotiatingViewResolver();
        this.contentNegotiatingResolver.setDefaultViews(Arrays.asList(defaultViews));
        this.contentNegotiatingResolver.setViewResolvers(this.viewResolvers);
        this.contentNegotiatingResolver.setContentNegotiationManager(this.contentNegotiationManager);
    }
}
Also used : ArrayList(java.util.ArrayList) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) View(org.springframework.web.servlet.View)

Example 3 with ContentNegotiatingViewResolver

use of org.springframework.web.servlet.view.ContentNegotiatingViewResolver in project spring-framework by spring-projects.

the class MvcNamespaceTests method testContentNegotiationManager.

@Test
public void testContentNegotiationManager() throws Exception {
    loadBeanDefinitions("mvc-config-content-negotiation-manager.xml", 15);
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
    NativeWebRequest webRequest = new ServletWebRequest(request);
    assertEquals(Collections.singletonList(MediaType.valueOf("application/rss+xml")), manager.resolveMediaTypes(webRequest));
    ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
    assertNotNull(compositeResolver);
    assertEquals("Actual: " + compositeResolver.getViewResolvers(), 1, compositeResolver.getViewResolvers().size());
    ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
    assertEquals(ContentNegotiatingViewResolver.class, resolver.getClass());
    ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
    assertSame(manager, cnvr.getContentNegotiationManager());
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) 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 4 with ContentNegotiatingViewResolver

use of org.springframework.web.servlet.view.ContentNegotiatingViewResolver 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 5 with ContentNegotiatingViewResolver

use of org.springframework.web.servlet.view.ContentNegotiatingViewResolver in project spring-framework by spring-projects.

the class ViewResolverRegistryTests method contentNegotiation.

@Test
public void contentNegotiation() {
    MappingJackson2JsonView view = new MappingJackson2JsonView();
    this.registry.enableContentNegotiation(view);
    ContentNegotiatingViewResolver resolver = checkAndGetResolver(ContentNegotiatingViewResolver.class);
    assertEquals(Arrays.asList(view), resolver.getDefaultViews());
    assertEquals(Ordered.HIGHEST_PRECEDENCE, this.registry.getOrder());
}
Also used : ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) Test(org.junit.Test)

Aggregations

ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)8 Test (org.junit.Test)5 MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)5 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)4 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 ArrayList (java.util.ArrayList)2 Bean (org.springframework.context.annotation.Bean)2 ContentNegotiationManagerFactoryBean (org.springframework.web.accept.ContentNegotiationManagerFactoryBean)2 View (org.springframework.web.servlet.View)2 ViewResolver (org.springframework.web.servlet.ViewResolver)2 BeanNameViewResolver (org.springframework.web.servlet.view.BeanNameViewResolver)2 ViewResolverComposite (org.springframework.web.servlet.view.ViewResolverComposite)2 FreeMarkerViewResolver (org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver)2 GroovyMarkupViewResolver (org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver)2 ScriptTemplateViewResolver (org.springframework.web.servlet.view.script.ScriptTemplateViewResolver)2 TilesViewResolver (org.springframework.web.servlet.view.tiles3.TilesViewResolver)2 MarshallingView (org.springframework.web.servlet.view.xml.MarshallingView)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 ConversionServiceFactoryBean (org.springframework.context.support.ConversionServiceFactoryBean)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1