org.springframework.web.servlet.view.ContentNegotiatingViewResolver Java Examples

The following examples show how to use org.springframework.web.servlet.view.ContentNegotiatingViewResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testContentNegotiationManager() throws Exception {
	loadBeanDefinitions("mvc-config-content-negotiation-manager.xml");

	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());
}
 
Example #2
Source File: ViewResolversBeanDefinitionParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private BeanDefinition createContentNegotiatingViewResolver(Element resolverElement, ParserContext context) {
	RootBeanDefinition beanDef = new RootBeanDefinition(ContentNegotiatingViewResolver.class);
	beanDef.setSource(context.extractSource(resolverElement));
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	MutablePropertyValues values = beanDef.getPropertyValues();

	List<Element> elements = DomUtils.getChildElementsByTagName(resolverElement, new String[] {"default-views"});
	if (!elements.isEmpty()) {
		ManagedList<Object> list = new ManagedList<Object>();
		for (Element element : DomUtils.getChildElementsByTagName(elements.get(0), "bean", "ref")) {
			list.add(context.getDelegate().parsePropertySubElement(element, null));
		}
		values.add("defaultViews", list);
	}
	if (resolverElement.hasAttribute("use-not-acceptable")) {
		values.add("useNotAcceptableStatusCode", resolverElement.getAttribute("use-not-acceptable"));
	}
	Object manager = MvcNamespaceUtils.getContentNegotiationManager(context);
	if (manager != null) {
		values.add("contentNegotiationManager", manager);
	}
	return beanDef;
}
 
Example #3
Source File: ViewResolverRegistry.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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<View>(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);
	}
}
 
Example #4
Source File: DispatcherServletConfiguration.java    From activiti6-boot2 with Apache License 2.0 6 votes vote down vote up
@Bean
public ViewResolver contentNegotiatingViewResolver() {
    log.debug("Configuring the ContentNegotiatingViewResolver");
    ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
    List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();

    UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
    urlBasedViewResolver.setViewClass(JstlView.class);
    urlBasedViewResolver.setPrefix("/WEB-INF/pages/");
    urlBasedViewResolver.setSuffix(".jsp");
    viewResolvers.add(urlBasedViewResolver);

    viewResolver.setViewResolvers(viewResolvers);

    List<View> defaultViews = new ArrayList<View>();
    defaultViews.add(new MappingJackson2JsonView());
    viewResolver.setDefaultViews(defaultViews);

    return viewResolver;
}
 
Example #5
Source File: ViewResolverRegistry.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void initContentNegotiatingViewResolver(View[] defaultViews) {

		// ContentNegotiatingResolver in the registry: elevate its precedence!
		this.order = (this.order == null ? Ordered.HIGHEST_PRECEDENCE : this.order);

		if (this.contentNegotiatingResolver != null) {
			if (!ObjectUtils.isEmpty(defaultViews)) {
				if (!CollectionUtils.isEmpty(this.contentNegotiatingResolver.getDefaultViews())) {
					List<View> views = new ArrayList<View>(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);
		}
	}
 
Example #6
Source File: ViewResolversBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private BeanDefinition createContentNegotiatingViewResolver(Element resolverElement, ParserContext context) {
	RootBeanDefinition beanDef = new RootBeanDefinition(ContentNegotiatingViewResolver.class);
	beanDef.setSource(context.extractSource(resolverElement));
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	MutablePropertyValues values = beanDef.getPropertyValues();

	List<Element> elements = DomUtils.getChildElementsByTagName(resolverElement, new String[] {"default-views"});
	if (!elements.isEmpty()) {
		ManagedList<Object> list = new ManagedList<Object>();
		for (Element element : DomUtils.getChildElementsByTagName(elements.get(0), "bean", "ref")) {
			list.add(context.getDelegate().parsePropertySubElement(element, null));
		}
		values.add("defaultViews", list);
	}
	if (resolverElement.hasAttribute("use-not-acceptable")) {
		values.add("useNotAcceptableStatusCode", resolverElement.getAttribute("use-not-acceptable"));
	}
	Object manager = getContentNegotiationManager(context);
	if (manager != null) {
		values.add("contentNegotiationManager", manager);
	}
	return beanDef;
}
 
Example #7
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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(Arrays.asList(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());
}
 
Example #8
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml", 7);

	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(7, 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));
}
 
Example #9
Source File: DelegatingCatnapWebMvcConfiguration.java    From catnap with Apache License 2.0 6 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    List<View> defaultViews = new ArrayList<>(2);
    defaultViews.add(jsonCatnapSpringView());
    defaultViews.add(jsonpCatnapSpringView());

    List<CatnapWrappingView> catnapViews = new ArrayList<>(2);
    catnapViews.add(jsonCatnapSpringView());
    catnapViews.add(jsonpCatnapSpringView());

    CatnapViewResolver catnapViewResolver = new CatnapViewResolver();
    catnapViewResolver.setViews(catnapViews);

    List<ViewResolver> viewResolvers = new ArrayList<>(1);
    viewResolvers.add(catnapViewResolver);

    ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver();
    cnvr.setContentNegotiationManager(mvcContentNegotiationManager());
    cnvr.setOrder(1);
    cnvr.setDefaultViews(defaultViews);
    cnvr.setViewResolvers(viewResolvers);

    return cnvr;
}
 
Example #10
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml");

	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"));
}
 
Example #11
Source File: ViewResolversBeanDefinitionParser.java    From java-technology-stack with MIT License 6 votes vote down vote up
private BeanDefinition createContentNegotiatingViewResolver(Element resolverElement, ParserContext context) {
	RootBeanDefinition beanDef = new RootBeanDefinition(ContentNegotiatingViewResolver.class);
	beanDef.setSource(context.extractSource(resolverElement));
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	MutablePropertyValues values = beanDef.getPropertyValues();

	List<Element> elements = DomUtils.getChildElementsByTagName(resolverElement, "default-views");
	if (!elements.isEmpty()) {
		ManagedList<Object> list = new ManagedList<>();
		for (Element element : DomUtils.getChildElementsByTagName(elements.get(0), "bean", "ref")) {
			list.add(context.getDelegate().parsePropertySubElement(element, null));
		}
		values.add("defaultViews", list);
	}
	if (resolverElement.hasAttribute("use-not-acceptable")) {
		values.add("useNotAcceptableStatusCode", resolverElement.getAttribute("use-not-acceptable"));
	}
	Object manager = MvcNamespaceUtils.getContentNegotiationManager(context);
	if (manager != null) {
		values.add("contentNegotiationManager", manager);
	}
	return beanDef;
}
 
Example #12
Source File: ViewResolverRegistry.java    From java-technology-stack with MIT License 6 votes vote down vote up
private ContentNegotiatingViewResolver 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) &&
				!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);
		if (this.contentNegotiationManager != null) {
			this.contentNegotiatingResolver.setContentNegotiationManager(this.contentNegotiationManager);
		}
	}
	return this.contentNegotiatingResolver;
}
 
Example #13
Source File: WidgetConfiguration.java    From catnap with Apache License 2.0 6 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    List<View> defaultViews = new ArrayList<>(2);
    defaultViews.add(jsonCatnapSpringView());
    defaultViews.add(jsonpCatnapSpringView());

    List<CatnapWrappingView> catnapViews = new ArrayList<>(2);
    catnapViews.add(jsonCatnapSpringView());
    catnapViews.add(jsonpCatnapSpringView());

    CatnapViewResolver catnapViewResolver = new CatnapViewResolver();
    catnapViewResolver.setViews(catnapViews);

    List<ViewResolver> viewResolvers = new ArrayList<>(1);
    viewResolvers.add(catnapViewResolver);

    ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver();
    cnvr.setContentNegotiationManager(mvcContentNegotiationManager());
    cnvr.setOrder(1);
    cnvr.setDefaultViews(defaultViews);
    cnvr.setViewResolvers(viewResolvers);

    return cnvr;
}
 
Example #14
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testViewResolutionWithContentNegotiation() throws Exception {
	loadBeanDefinitions("mvc-config-view-resolution-content-negotiation.xml");

	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"));
}
 
Example #15
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testContentNegotiationManager() throws Exception {
	loadBeanDefinitions("mvc-config-content-negotiation-manager.xml");

	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());
}
 
Example #16
Source File: ViewResolversBeanDefinitionParser.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private BeanDefinition createContentNegotiatingViewResolver(Element resolverElement, ParserContext context) {
	RootBeanDefinition beanDef = new RootBeanDefinition(ContentNegotiatingViewResolver.class);
	beanDef.setSource(context.extractSource(resolverElement));
	beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	MutablePropertyValues values = beanDef.getPropertyValues();

	List<Element> elements = DomUtils.getChildElementsByTagName(resolverElement, "default-views");
	if (!elements.isEmpty()) {
		ManagedList<Object> list = new ManagedList<>();
		for (Element element : DomUtils.getChildElementsByTagName(elements.get(0), "bean", "ref")) {
			list.add(context.getDelegate().parsePropertySubElement(element, null));
		}
		values.add("defaultViews", list);
	}
	if (resolverElement.hasAttribute("use-not-acceptable")) {
		values.add("useNotAcceptableStatusCode", resolverElement.getAttribute("use-not-acceptable"));
	}
	Object manager = MvcNamespaceUtils.getContentNegotiationManager(context);
	if (manager != null) {
		values.add("contentNegotiationManager", manager);
	}
	return beanDef;
}
 
Example #17
Source File: ViewResolverRegistry.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private ContentNegotiatingViewResolver 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) &&
				!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);
		if (this.contentNegotiationManager != null) {
			this.contentNegotiatingResolver.setContentNegotiationManager(this.contentNegotiationManager);
		}
	}
	return this.contentNegotiatingResolver;
}
 
Example #18
Source File: ApplicationConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver viewResolver(ContentNegotiationManager cnManager) {
    ContentNegotiatingViewResolver cnvResolver = new ContentNegotiatingViewResolver();
    cnvResolver.setContentNegotiationManager(cnManager);
    List<ViewResolver> resolvers = new ArrayList<>();

    InternalResourceViewResolver bean = new InternalResourceViewResolver("/WEB-INF/views/",".jsp");
    ArticleRssFeedViewResolver articleRssFeedViewResolver = new ArticleRssFeedViewResolver();

    resolvers.add(bean);
    resolvers.add(articleRssFeedViewResolver);

    cnvResolver.setViewResolvers(resolvers);
    return cnvResolver;
}
 
Example #19
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
Example #20
Source File: ViewResolutionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContentNegotiation() throws Exception {

	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<View>();
	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")));
}
 
Example #21
Source File: ViewResolverRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void contentNegotiationAddsDefaultViewRegistrations() {
	MappingJackson2JsonView view1 = new MappingJackson2JsonView();
	this.registry.enableContentNegotiation(view1);

	ContentNegotiatingViewResolver resolver1 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
	assertEquals(Arrays.asList(view1), resolver1.getDefaultViews());

	MarshallingView view2 = new MarshallingView();
	this.registry.enableContentNegotiation(view2);

	ContentNegotiatingViewResolver resolver2 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
	assertEquals(Arrays.asList(view1, view2), resolver2.getDefaultViews());
	assertSame(resolver1, resolver2);
}
 
Example #22
Source File: ViewResolverRegistryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #23
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
Example #24
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
Example #25
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
Example #26
Source File: ViewResolverRegistryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void contentNegotiationAddsDefaultViewRegistrations() {
	MappingJackson2JsonView view1 = new MappingJackson2JsonView();
	this.registry.enableContentNegotiation(view1);

	ContentNegotiatingViewResolver resolver1 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
	assertEquals(Arrays.asList(view1), resolver1.getDefaultViews());

	MarshallingView view2 = new MarshallingView();
	this.registry.enableContentNegotiation(view2);

	ContentNegotiatingViewResolver resolver2 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
	assertEquals(Arrays.asList(view1, view2), resolver2.getDefaultViews());
	assertSame(resolver1, resolver2);
}
 
Example #27
Source File: ViewResolverRegistryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #28
Source File: WebConfig.java    From Project with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver cnViewResolver(ContentNegotiationManager cnm) {
	/*
	 * 内容协商视图解析器
	 * 1.确定请求的媒体类型;
	 * 		首先查看URL的文件扩展名,如果扩展名是“.json”,那么所需的内容类型必须是application/json
	 * 		没找到扩展名,则从请求中的Accept头部信息中找
	 * 		都没有,ContentNegotiatingViewResolver将会使用“/”作为默认的内容类型,
	 * 			这就意味着客户端必须要接收服务器发送的任何形式的表述
	 * 2.找到适合请求媒体类型的最佳视图
	 */
	ContentNegotiatingViewResolver cnvr = new ContentNegotiatingViewResolver();
	cnvr.setContentNegotiationManager(cnm);
	return cnvr;
}
 
Example #29
Source File: ViewResolverRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void contentNegotiationAddsDefaultViewRegistrations() {
	MappingJackson2JsonView view1 = new MappingJackson2JsonView();
	this.registry.enableContentNegotiation(view1);

	ContentNegotiatingViewResolver resolver1 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
	assertEquals(Arrays.asList(view1), resolver1.getDefaultViews());

	MarshallingView view2 = new MarshallingView();
	this.registry.enableContentNegotiation(view2);

	ContentNegotiatingViewResolver resolver2 = checkAndGetResolver(ContentNegotiatingViewResolver.class);
	assertEquals(Arrays.asList(view1, view2), resolver2.getDefaultViews());
	assertSame(resolver1, resolver2);
}
 
Example #30
Source File: ViewResolverRegistryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}