当前位置: 首页>>代码示例>>Java>>正文


Java FixedContentNegotiationStrategy类代码示例

本文整理汇总了Java中org.springframework.web.accept.FixedContentNegotiationStrategy的典型用法代码示例。如果您正苦于以下问题:Java FixedContentNegotiationStrategy类的具体用法?Java FixedContentNegotiationStrategy怎么用?Java FixedContentNegotiationStrategy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


FixedContentNegotiationStrategy类属于org.springframework.web.accept包,在下文中一共展示了FixedContentNegotiationStrategy类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: nestedViewResolverIsNotSpringBean

import org.springframework.web.accept.FixedContentNegotiationStrategy; //导入依赖的package包/类
@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<ViewResolver>(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);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:ContentNegotiatingViewResolverTests.java

示例2: resolveViewNameWithDefaultContentType

import org.springframework.web.accept.FixedContentNegotiationStrategy; //导入依赖的package包/类
@Test
public void resolveViewNameWithDefaultContentType() throws Exception {
	request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

	MediaType mediaType = new MediaType("application", "xml");
	FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(mediaType);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

	ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
	ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
	viewResolver.afterPropertiesSet();

	View viewMock1 = mock(View.class, "application_xml");
	View viewMock2 = mock(View.class, "text_html");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
	given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
	given(viewMock1.getContentType()).willReturn("application/xml");
	given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock1, result);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:28,代码来源:ContentNegotiatingViewResolverTests.java

示例3: setDefaultContentTypeStrategy

import org.springframework.web.accept.FixedContentNegotiationStrategy; //导入依赖的package包/类
@Test
public void setDefaultContentTypeStrategy() throws Exception {
	this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
	ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();

	assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:8,代码来源:ContentNegotiationConfigurerTests.java

示例4: testContentNegotiation

import org.springframework.web.accept.FixedContentNegotiationStrategy; //导入依赖的package包/类
@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")));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:40,代码来源:ViewResolutionTests.java

示例5: testContentNegotiation

import org.springframework.web.accept.FixedContentNegotiationStrategy; //导入依赖的package包/类
@Test
public void testContentNegotiation() throws Exception {

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

	List<View> viewList = new ArrayList<View>();
	viewList.add(new MappingJacksonJsonView());
	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")));
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:40,代码来源:ViewResolutionTests.java


注:本文中的org.springframework.web.accept.FixedContentNegotiationStrategy类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。