(转)(记录)How exactly works the Spring @ResponseBody annotation in this RESTful app

参考:

http://stackoverflow.com/questions/28646332/how-exactly-works-the-spring-responsebody-annotation-in-this-restful-applicatio

当使用REST的时候,response json,问题是返回的json是如何对返回对象处理的?是将对象作为List处理么?

其实并不是。解答是:

First of all, the annotation doesn't annotate List. It annotates the method, just as RequestMappingdoes. Your code is equivalent to

@RequestMapping(value="/orders", method=RequestMethod.GET)
@ResponseBody
public List<Account> accountSummary() {
    return accountManager.getAllAccounts();
}

Now what the annotation means is that the returned value of the method will constitute the body of the HTTP response. Of course, an HTTP response can't contain Java objects. So this list of accounts is transformed to a format suitable for REST applications, typically JSON or XML.

The choice of the format depends on the installed message converters, on the values of the produces attribute of the RequestMapping annotation, and on the content type that the client accepts (that is available in the HTTP request headers). For example, if the request says it accepts XML, but not JSON, and there is a message converter installed that can transform the list to XML, then XML will be returned.

猜你喜欢

转载自jameskaron.iteye.com/blog/2311305