@RequestMapping中的produces的作用和使用方式


轉發:原博客

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")  

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM