`

spring mvc设置应答体的content type

阅读更多

spring MVC中如何设置应答体的content type呢?

spring MVC中如何设置返回类型呢?

我们知道response 的content type主要有:

text/html

text/plain

application/json;charset=UTF-8

application/octet-stream

先举一个例子,spring mvc中可以通过如下方式返回json字符串:

@ResponseBody
	@RequestMapping(value = "/upload")
	public String upload(HttpServletRequest request, HttpServletResponse response,String contentType2)
			throws IOException {
		String content = null;
		Map map = new HashMap();
		ObjectMapper mapper = new ObjectMapper();

		map.put("fileName", "a.txt");
		try {
			content = mapper.writeValueAsString(map);
			System.out.println(content);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return content;

	}

 虽然访问时返回的确实是json字符串,但是response 的content type是"

text/html

"这不是我们期望的,我们期望的response content type是"application/json"或者"application/json;charset=UTF-8",那么如何实现呢?

通过注解@RequestMapping 中的produces

用法如下:

@RequestMapping(value = "/upload",produces="application/json;charset=UTF-8")

 spring MVC官方文档:

Producible Media Types

You can narrow the primary mapping by specifying a list of producible media types. The request will be matched only if the Accept request header matches one of these values. Furthermore, use of the produces condition ensures the actual content type used to generate the response respects the media types specified in the producescondition. For example:

@Controller
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
    // implementation omitted
}

Just like with consumes, producible media type expressions can be negated as in !text/plain to match to all requests other than those with an Accept header value oftext/plain.

Tip
[Tip]

The produces condition is supported on the type and on the method level. Unlike most other conditions, when used at the type level, method-level producible types override rather than extend type-level producible types.

 

 

 

1
2
分享到:
评论
7 楼 xinxinlong 2016-03-14  
@ResponseBody
@RequestMapping(value="/getResourceTree",consumes="application/json;charset=UTF-8",produces="application/json;charset=UTF-8")
6 楼 xinxinlong 2016-03-14  
hw1287789687 写道
xinxinlong 写道
为何设置不起作用!


请问怎么设置的

produces="application/json;charset=UTF-8" 直接写上的这个注解,结果返回依然是text/html
5 楼 hw1287789687 2016-03-13  
xinxinlong 写道
为何设置不起作用!


请问怎么设置的
4 楼 xinxinlong 2016-03-11  
为何设置不起作用!
3 楼 hw1287789687 2015-03-04  
@ResponseBody
	@RequestMapping(value = "/json2",produces=SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF )
	public String upload(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String content = null;
		Map map = new HashMap();
		ObjectMapper mapper = new ObjectMapper();

		map.put("fileName", "a.txt");
		try {
			content = mapper.writeValueAsString(map);
			System.out.println(content);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return content;

	}
2 楼 devilyard 2015-03-04  
原来可以这样,受教了

相关推荐

    精通Spring MVC 4

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。Spring MVC4是当前zuixin的版本,在众多特性上有了进一步的提升。, 在精通Spring...

    精通Spring MVC 4 中文

    精通Spring MVC 4 中文

    spring mvc 官方文档

    本文详细介绍spring MVC的原理和开发心得体会。

    Spring MVC 基于注解实例

    Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...

    Spring MVC 入门实例

    首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...

    [免费]Spring MVC学习指南(高清)

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    spring mvc源代码

    spring mvc4.1.4 源代码 spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码spring mvc4.1.4 源代码

    Spring MVC所需jar包

    Spring MVC所需jar包,包含java开发中 Spring MVC架构中最常用的jar包

    Spring MVC jar包

    关于构建Sping MVC的Jar包,包括Sping2.5.6和Hibernate3.6.8

    Spring MVC 4.2.3

    Spring mvc jar包

    Spring MVC+MyBatis开发从入门到项目实战

    《Spring MVC+MyBatis开发从入门到项目实战》分为4篇。第1篇是Java开发环境的搭建,包括JDK的下载与安装、环境变量的配置、MyEclipse的下载与基本配置。第2篇是MyBatis技术入门,包括剖析JDBC的弊端、MyBatis的背景...

    大优惠 Spring MVC学习指南(第2版)2017.pdf

    Spring MVC是Spring框架中用于Web应用快速开发的一个模块,其中的MVC是Model-View-Controller的缩写。作为当今业界最主流的Web开发框架,Spring MVC已经成为当前最热门的开发技能,同时也广泛用于桌面开发领域。 ...

    Servlet JSP和Spring MVC初学指南

    Servlet JSP和Spring MVC初学指南

    spring MVC数据绑定大全

    spring MVC数据绑定 含例子 转载自疯芒毕露的专栏 刚开始用spring mvc 做web开发时 经常会不知道如何合适绑定页面数据 用惯struts2的朋友更认为spring mvc 绑定数据不如struts2方便 本人最开始也是这么认为 经过一段...

    Spring MVC MyBatis开发从入门到项目实战

    Spring MVC MyBatis开发从入门到项目实战

    Spring mvc 教程

    很有用的Spring mvc中文文档

    spring mvc框架依赖全面jar

    spring mvc轻量级框架搭建,依赖全面jar文件包。下载解压直接将jar文件复制到工程中的lib中。

    Spring MVC入门教程

    二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 双亲上下文的说明 六、springMVC-mvc.xml 配置文件片段讲解 七、spring mvc 如何访问到静态的文件,...

Global site tag (gtag.js) - Google Analytics