1塞帐、
@responseBody注解的作用是將controller的方法返回的對象通過適當(dāng)?shù)霓D(zhuǎn)換器轉(zhuǎn)換為指定的格式之后流部,寫入到response對象的body區(qū),通常用來返回JSON數(shù)據(jù)或者是XML
數(shù)據(jù),需要注意的呢愉老,在使用此注解之后不會再走試圖處理器舌稀,而是直接將數(shù)據(jù)寫入到輸入流中啊犬,他的效果等同于通過response對象輸出指定格式的數(shù)據(jù)。
2壁查、
@RequestMapping("/login")
@ResponseBody
public User login(User user){
return user;
}
User字段:userName pwd 那么在前臺接收到的數(shù)據(jù)為:'{"userName":"xxx","pwd":"xxx"}'
效果等同于如下代碼:
@RequestMapping("/login")
public void login(User user, HttpServletResponse response){
response.getWriter.write(JSONObject.fromObject(user).toString());
}