spring mvc设置应答体的content type
来源:互联网 发布:linux基础教程第二版 编辑:程序博客网 时间:2024/06/07 06:17
spring MVC中如何设置应答体的 content type 呢?
我们知道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 produces condition. For example:
@Controller@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")@ResponseBodypublic 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 of text/plain .
TipThe 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.
- spring mvc设置应答体的content type
- Spring MVC中如何设置应答体的content type
- Spring mvc 单个@RequestMapping返回特定Content-Type的配置
- Spring MVC关于IE对application/json的content-type不支持解决方案---duang 解决了
- Spring MVC的@ResponseBody返回JSON串时Content-Type编码问题
- 码农小汪-Spring MVC -生产者、消费者限定 Content-Type Accept
- 设置content type
- OkHttp3设置Content-Type
- OkHttp3设置Content-Type
- Content-Type:一览 MVC FileResult
- Spring MVC Content Negotiation
- php中利用header设置content-type和常见文件类型的content-type
- php中利用header设置content-type和常见文件类型的content-type
- Spring Content type XXX 不支持
- http Content-type及Spring的@RequestBody注解
- ASI框架设置Content-Type无效的问题
- php的header函数之设置content-type
- php的header函数之设置content-type
- Unity问题集合
- Length of Last Word
- 学习Android中广播的总结
- Android项目重构之路:架构篇
- .NetCore~C#6的一些新特性
- spring mvc设置应答体的content type
- 系统广播
- android studio 出现Error:Execution failed for task ':app:buildInfoDebugLoader'.的解决办法
- java基础 --自己随手复习
- Android系统广播大全及开机自启动的Service
- linux下编译php中configure参数具体含义
- less简介
- IO流_FileOutputStream写出数据
- List 1 17.02.07