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


Java ContentNegotiationManagerFactoryBean类代码示例

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


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

示例1: responseBodyAsHtml

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
@Test
public void responseBodyAsHtml() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertEquals("inline;filename=f.txt", response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:ServletAnnotationControllerHandlerMethodTests.java

示例2: responseBodyAsHtmlWithSuffixPresent

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
@Test
public void responseBodyAsHtmlWithSuffixPresent() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:ServletAnnotationControllerHandlerMethodTests.java

示例3: responseBodyAsHtmlWithProducesCondition

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
@Test
public void responseBodyAsHtmlWithProducesCondition() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:ServletAnnotationControllerHandlerMethodTests.java

示例4: responseBodyAsTextWithCssExtension

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
@Test
public void responseBodyAsTextWithCssExtension() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "body".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/css", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:ServletAnnotationControllerHandlerMethodTests.java

示例5: getContentNegotiationManager

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
private RuntimeBeanReference getContentNegotiationManager(Element element, Object source, ParserContext parserContext) {
	RuntimeBeanReference contentNegotiationManagerRef;
	if (element.hasAttribute("content-negotiation-manager")) {
		contentNegotiationManagerRef = new RuntimeBeanReference(element.getAttribute("content-negotiation-manager"));
	}
	else {
		RootBeanDefinition factoryBeanDef = new RootBeanDefinition(ContentNegotiationManagerFactoryBean.class);
		factoryBeanDef.setSource(source);
		factoryBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		factoryBeanDef.getPropertyValues().add("mediaTypes", getDefaultMediaTypes());

		String beanName = "mvcContentNegotiationManager";
		parserContext.getReaderContext().getRegistry().registerBeanDefinition(beanName , factoryBeanDef);
		parserContext.registerComponent(new BeanComponentDefinition(factoryBeanDef, beanName));
		contentNegotiationManagerRef = new RuntimeBeanReference(beanName);
	}
	return contentNegotiationManagerRef;
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:19,代码来源:AnnotationDrivenBeanDefinitionParser.java

示例6: createResponseWriter

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
protected HttpMessageConvertingResponseWriter createResponseWriter() {
    ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
    factoryBean.afterPropertiesSet();

    ContentNegotiationManager contentNegotiationManager = factoryBean.getObject();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();

    List<JsonSerializer<?>> serializers = new ArrayList<>();
    serializers.add(new ObjectIdSerializer());

    Map<Class<?>, JsonDeserializer<?>> deserializers = new HashMap<>();
    deserializers.put(ObjectId.class, new ObjectIdDeserializer());

    CustomSerializationObjectMapper objectMapper = new CustomSerializationObjectMapper();
    objectMapper.setSerializers(serializers);
    objectMapper.setDeserializers(deserializers);
    objectMapper.init();

    MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(objectMapper);

    messageConverters.add(jsonMessageConverter);

    return new HttpMessageConvertingResponseWriter(contentNegotiationManager, messageConverters);
}
 
开发者ID:craftercms,项目名称:profile,代码行数:26,代码来源:AbstractRestHandlerTestBase.java

示例7: contentNegotiationManager

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
@Bean
@ConfigurationProperties("spring.mvc.viewManager")
public ContentNegotiationManager contentNegotiationManager() {
	// 声明contentNegotiationManagerFactory工厂实例,并根据配置参数填充对象。
	ContentNegotiationManagerFactoryBean contentNegotiationManagerFactory = new ContentNegotiationManagerFactoryBean();
	// 手动填充mediaTypes参数
	Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>();
	mediaTypes.put("json", MediaType.APPLICATION_JSON_UTF8);
	mediaTypes.put("xml", MediaType.APPLICATION_XML);
	contentNegotiationManagerFactory.addMediaTypes(mediaTypes);
	// 返回contentNegotiationManager对象
	return contentNegotiationManagerFactory.getObject();
}
 
开发者ID:phoenix-varus,项目名称:jeeWe,代码行数:14,代码来源:MvcViewConfiguration.java

示例8: addContentDispositionHeader

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

	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.addMediaType("pdf", new MediaType("application", "pdf"));
	factory.afterPropertiesSet();

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			Collections.singletonList(new StringHttpMessageConverter()),
			factory.getObject());

	assertContentDisposition(processor, false, "/hello.json", "whitelisted extension");
	assertContentDisposition(processor, false, "/hello.pdf", "registered extension");
	assertContentDisposition(processor, true, "/hello.dataless", "uknown extension");

	// path parameters
	assertContentDisposition(processor, false, "/hello.json;a=b", "path param shouldn't cause issue");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "uknown ext in path params");
	assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "uknown ext in filename");
	assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "whitelisted extensions");

	// encoded dot
	assertContentDisposition(processor, true, "/hello%2Edataless;a=b;setup.json", "encoded dot in filename");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup%2Edataless", "encoded dot in path params");
	assertContentDisposition(processor, true, "/hello.dataless%3Bsetup.bat", "encoded dot in path params");

	this.servletRequest.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/hello.bat");
	assertContentDisposition(processor, true, "/bonjour", "forwarded URL");
	this.servletRequest.removeAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:31,代码来源:RequestResponseBodyMethodProcessorTests.java

示例9: mvcContentNegotiationManager

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
/**
 * 内容协商
 * @return
 */
@Bean
public ContentNegotiationManager mvcContentNegotiationManager() {
    ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean = new ContentNegotiationManagerFactoryBean();
    contentNegotiationManagerFactoryBean.setFavorParameter(true);
    contentNegotiationManagerFactoryBean.setIgnoreAcceptHeader(true);
    contentNegotiationManagerFactoryBean.setDefaultContentType(MediaType.APPLICATION_JSON_UTF8);
    Properties mediaTypesProperties = new Properties();
    mediaTypesProperties.setProperty("json", MediaType.APPLICATION_JSON_UTF8_VALUE);
    contentNegotiationManagerFactoryBean.setMediaTypes(mediaTypesProperties);
    contentNegotiationManagerFactoryBean.afterPropertiesSet();
	return contentNegotiationManagerFactoryBean.getObject();
}
 
开发者ID:liangzai-cool,项目名称:loginsecuritybyredis,代码行数:17,代码来源:WebAppConfig.java

示例10: contentNegotiationManager

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
@Bean
public ContentNegotiationManager contentNegotiationManager() {
	ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean = new ContentNegotiationManagerFactoryBean();
	contentNegotiationManagerFactoryBean.setFavorPathExtension(true);
	contentNegotiationManagerFactoryBean.setFavorParameter(false);

	// contentNegotiationManagerFactoryBean.addMediaType("json", MediaType.APPLICATION_JSON);
	// contentNegotiationManagerFactoryBean.addMediaType("html", MediaType.TEXT_HTML);
	// contentNegotiationManagerFactoryBean.setDefaultContentType(MediaType.APPLICATION_JSON);

	contentNegotiationManagerFactoryBean.setMediaTypes(mediaTypes);
	contentNegotiationManagerFactoryBean.setDefaultContentType(defaultContentType);

	return contentNegotiationManagerFactoryBean.getObject();
}
 
开发者ID:sdcuike,项目名称:spring4-2015,代码行数:16,代码来源:WebConfig.java

示例11: contentNegotiationManagerFactory

import org.springframework.web.accept.ContentNegotiationManagerFactoryBean; //导入依赖的package包/类
@Bean
public ContentNegotiationManagerFactoryBean contentNegotiationManagerFactory() {
	ContentNegotiationManagerFactoryBean fac = new ContentNegotiationManagerFactoryBean();
	fac.setDefaultContentType(MediaType.APPLICATION_JSON);
	fac.addMediaType("json", MediaType.APPLICATION_JSON);
	fac.addMediaType("xml", MediaType.APPLICATION_XML);
	return fac;
}
 
开发者ID:JensKapitza,项目名称:jsf-exmple,代码行数:9,代码来源:PersistenceConfig.java


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