原文地址:https://blog.csdn.net/u010127245/article/details/51774074
一、@RequestMapping
RequestMapping是一個用來處理請求地址映射的注解,可用於類或方法上。用於類上,表示類中的所有響應請求的方法都是以該地址作為父路徑。
RequestMapping注解有六個屬性,下面我們把她分成三類進行說明。
1、 value, method;
value: 指定請求的實際地址,指定的地址可以是URI Template 模式(后面將會說明);
method: 指定請求的method類型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定處理請求的提交內容類型(Content-Type),例如application/json, text/html;
produces: 指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回;
3、 params,headers;
params: 指定request中必須包含某些參數值是,才讓該方法處理。
headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。
二、json數據亂碼
在springMVC controller中返回json數據出現亂碼問題,因為沒有進行編碼,只需要簡單的注解就可以了
在@RequestMapping()中加入
produces="text/html;charset=UTF-8"
屬性即可,如下:
-
<span style=
"font-size:18px;">
@RequestMapping(value=
"/respost",method=RequestMethod.GET,produces=
"text/html;charset=UTF-8")
-
@ResponseBody
-
public String postList(@RequestParam("topicId") String topicId){
-
List<Post> posts=
new ArrayList<Post>();
-
System.out.println(
"topicId-----"+topicId);
-
posts=postService.findPostList(topicId);
-
JSONArray postJson=JSONArray.fromObject(posts);
-
return postJson.toString();
-
}</span>