{"id":3124,"date":"2019-03-02T11:53:26","date_gmt":"2019-03-02T11:53:26","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3124"},"modified":"2019-03-02T11:54:36","modified_gmt":"2019-03-02T11:54:36","slug":"how-requestbody-and-responsebody-works","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/how-requestbody-and-responsebody-works\/","title":{"rendered":"How @RequestBody and @ResponseBody Works"},"content":{"rendered":"<p>@RequestBody, spring will try to convert the content of the incoming request body to your parameter object on the fly.@ResponseBody, spring will try to convert its return value and write it to the http response automatically<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Controller\r\n@RequestMapping(value = &quot;\/bookcase&quot;)\r\npublic class BookCaseController \r\n{ \r\n    private BookCase bookCase;\r\n \r\n    @RequestMapping(method = RequestMethod.GET)\r\n    @ResponseBody\r\n    public BookCase getBookCase() {\r\n        return this.bookCase;\r\n    }\r\n \r\n    @RequestMapping(method = RequestMethod.PUT)\r\n    @ResponseStatus(HttpStatus.NO_CONTENT)\r\n    public void setBookCase(@RequestBody BookCase bookCase) {\r\n        this.bookCase = bookCase;\r\n    } \r\n}\r\n<\/pre>\n<p>Depending on your configuration, spring has a list of HttpMessageConverters registered in the background. A HttpMessageConverters responsibility is to convert the request body to a specific class and back to the response body again, depending on a predefined mime type. Every time an issued request is hitting a @RequestBody or @ResponseBody annotation spring loops through all registered HttpMessageConverters seeking for the first that fits the given mime type and class and then uses it for the actual conversion.<\/p>\n<p>Refer <a href=\"https:\/\/www.javacodegeeks.com\/2013\/07\/spring-mvc-requestbody-and-responsebody-demystified.html\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>@RequestBody, spring will try to convert the content of the incoming request body to your parameter object on the fly.@ResponseBody, spring will try to convert its return value and write it to the http response automatically @Controller @RequestMapping(value = &quot;\/bookcase&quot;) public class BookCaseController { private BookCase bookCase; @RequestMapping(method = RequestMethod.GET) @ResponseBody public BookCase getBookCase() {&hellip; <a href=\"https:\/\/codethataint.com\/blog\/how-requestbody-and-responsebody-works\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[264],"tags":[],"class_list":["post-3124","post","type-post","status-publish","format-standard","hentry","category-rest"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3124"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3124\/revisions"}],"predecessor-version":[{"id":3130,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3124\/revisions\/3130"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}