@ResponseBody
@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文(ResponseBody)中,一般在异步获取数据时使用,通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中。
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用。例如(当请求发送http://localhost:9555/mavenweb/index.do时进入页面(此时该controller对应的方法不加ResponseBody,若加了,则不会进入页面,只有一个空白页,显示方法中返回的字符串),当加载该页面的时候会发送一个请求去请求后台数据显示在页面,此时要加RequestBody,因为我们的页面已经加载出来,要显示数据,所以我们返回到前端的因该是一个json字符串);
但是,例如在点击超链接删除一个用户后,controller方法返回值为string,如果不添加该注解,则页面不会自动跳转
@Controller
public class controller {
@Autowired
private CustomerService customerService;
// 这个方法不是异步请求,但是我们要的是将对象转换成json数据返回到页面,是直接显示在了一个空白页面上,没有定义任何
// 页面,可以用model+页面将数据显示到页面上,这时候应该就不需要response了
// 所以就添加@responseBody这个注解,他可以将java对象转换成json数据,反之亦可
@RequestMapping("/customer/list")
@ResponseBody
public Customer showCustomer(Model model){
System.out.print("===============");
// List<Customer> customerList = customerService.getCustomerList();
Customer customer = customerService.getCustomer();
System.out.print(customer+"===============");
return customer;
}
// 异步请求就要@response
//不加的话就会进行页面跳转
// 这里如果添加了@responsebody
// 结果:整个页面只有:"customer" 字符串
// 因为@responsebody 返回的数据不是html页面而是其他格式的数据,注意它直接将你返回的东西即retur后的字符串
// 返回到了页面,例如json,xml等,这里呢,我们自己将数据进行处理,然后在页面中显示,springmvc的视图解析器,会
// 将数据解析然后放到页面模板里结合,与reponsebody没关系了
@RequestMapping("/customer/list2")
public String showCustomerList(Model model){
System.out.print("LIST+++++++++++++++++++++");
// List<Customer> customerList = customerService.getCustomerList();
List<Customer> customerList = customerService.getCustomerList();
model.addAttribute("customerList",customerList);
System.out.print(customerList+"LIST+++++++++++++++++++++");
return "customers";
}
// 在前台页面我们点击超链接删除,是$.post也就是ajax请求,即异步请求
// 异步请求就的用response
// 根据id删除用户,这里如果不加@ResponseBody则数据可以被删除,但是页面不会进行自动刷新。
@RequestMapping("/customer/delete")
@ResponseBody
public String deleteCustomer(Long id){
customerService.deleteCustomer(id);
System.out.print("删除成功.....");
return "OK";
}
这里的ResponseBody会将java对象转换成json,前端是html,然后$.ajax的回调函数会将数据拿到
@RequestMapping("/customer/ht")
@ResponseBody
public Customer htm(HttpServletResponse response) throws Exception{
Customer customer = customerService.getCustomer();
System.out.print("html..............");
return customer;
}
// 点击按钮循环显示出所有数据到html页面,这里的@Response将数据转换成json格式,然后,就没它啥事了,到此这个controller方法就算是执行完毕了,然后前端的$.ajax的回调函数会拿到这个数据,然后进行操作
@RequestMapping("/customer/hts")
@ResponseBody
public List<Customer> htms(){
List<Customer> customerList = customerService.getCustomerList();
System.out.println(customerList);
return customerList;
}
}
html:
$.ajax({
type:"get",
url:"/mavenweb/customer/ht.do",
dateType:"json",
success:function (data) {
var retData = eval("(" + data + ")");
$("#username").val(retData.cust_name);
$("#address").val(retData.cust_address);
}
});
注:如果加@Requestmapping,然后返回值为 return “a”,那么它会将数据封装到页面,然后跳转到一个名字为a的页面。
如果@RequestMapping,又添加了@ResponseBody,如果此时返回值仍然为“a”,那么它就不会跳转页面了,只在一个空白页面显示a这个字符。
如果要想在这连个注解同时存在的情况下,跳转页面(就不要返回“字符串”),即返回值是什么类型,return什么类型,如 return costomer(对象)。此时的requestMapping,将数据准换成json,返回给前台。
总结:执行跳转页面功能的为requestmapping而非responseBody。即通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上 @Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中