轉發:原博客
1.他的作用是指定返回值類型和返回值編碼
2.consumes: 指定處理請求的提交內容類型(Content-Type),例如application/json, text/html;
一、produces的例子
produces第一種使用,返回json數據,下邊的代碼可以省略produces屬性,因為我們已經使用了注解@responseBody就是返回值是json數據:
1 @Controller 2 @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json") 3 @ResponseBody
produces第二種使用,返回json數據的字符編碼為utf-8.:
1 @Controller 2 @RequestMapping(value = "/pets/{petId}", produces="MediaType.APPLICATION_JSON_VALUE"+";charset=utf-8") 3 @ResponseBody
二、consumes的例子(方法僅處理request Content-Type為“application/json”類型的請求。指定處理請求的 提交內容類型 (Content-Type))
1 @Controller 2@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")