{"id":3175,"date":"2019-03-08T05:35:53","date_gmt":"2019-03-08T05:35:53","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3175"},"modified":"2019-03-16T17:36:24","modified_gmt":"2019-03-16T17:36:24","slug":"spring-rest-annotations","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-rest-annotations\/","title":{"rendered":"Spring REST Annotations"},"content":{"rendered":"<table>\n<thead>\n<tr>\n<th>Annotation<\/th>\n<th>Usage<\/th>\n<\/tr>\n<\/thead>\n<tr>\n<td>@RequestMapping<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@RequestMapping(value = &quot;\/{name}&quot;, \r\n                method = RequestMethod.GET, \r\n                consumes=&quot;application\/json&quot;\r\n                produces =&quot;application\/json&quot;,\r\n                headers={&quot;name=pankaj&quot;, &quot;id=1&quot;})\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">path (or) (or) name (or) and value: which URL the method is mapped to<br \/>\nmethod: compatible HTTP methods<br \/>\nparams: filters requests based on presence, absence, or value of HTTP parameters<br \/>\nheaders: filters requests based on presence, absence, or value of HTTP headers<br \/>\nconsumes: which media types the method can consume in the HTTP request body<br \/>\nproduces: which media types the method can produce in the HTTP response body<\/td>\n<\/tr>\n<tr>\n<td>@RequestBody<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@RequestMapping(method = RequestMethod.POST)\r\n@ResponseBody\r\npublic HttpStatus something(@RequestBody MyModel myModel) \r\n{\r\n    return HttpStatus.OK;\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">with @RequestBody, Spring will bind the incoming HTTP request body(for the URL mentioned in @RequestMapping for that method) to that parameter. While doing that, Spring will [behind the scenes] use HTTP Message converters to convert the HTTP request body into domain object [deserialize request body to domain object], based on Accept header present in request.<\/td>\n<\/tr>\n<tr>\n<td>@ResponseBody<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@RequestMapping(value = &quot;\/user\/all&quot;, method = RequestMethod.GET)\r\npublic @ResponseBody List&lt;User&gt; listAllUsers() {\r\n    return userService.findAllUsers();\r\n}\r\n<\/pre>\n<\/tr>\n<tr>\n<td colspan=\"2\">with @ResponseBody, Spring will bind the return value to outgoing HTTP response body. While doing that, Spring will [behind the scenes] use HTTP Message converters to convert the return value to HTTP response body\u00a0[serialize the object to response body], based on\u00a0Content-Type\u00a0present in request HTTP header<\/td>\n<\/tr>\n<tr>\n<td>@RequestParam<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:8080\/springmvc\/hello\/101?param1=10&param2=20\r\n\r\npublic String getDetails(\r\n    @RequestParam(value=&quot;param1&quot;, required=true) String param1,\r\n        @RequestParam(value=&quot;param2&quot;, required=false) String param2){\r\n...\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">@RequestParam is to obtain an parameter from the URI as well.@RequestParam annotation used for accessing the query parameter values from the request<br \/>\ndefaultValue \u2013 This is the default value as a fallback mechanism if request is not having the value or it is empty.<br \/>\nname \u2013 Name of the parameter to bind<br \/>\nrequired \u2013 Whether the parameter is mandatory or not. If it is true, failing to send that parameter will fail.<br \/>\nvalue \u2013 This is an alias for the name attribute<\/td>\n<\/tr>\n<p>&#8221;<\/p>\n<tr>\n<td>@PathVariable<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n'http:\/\/localhost:8080\/springmvc\/hello\/101?param1=10&amp;param2=20\r\n\r\n@RequestMapping(&quot;\/hello\/{id}&quot;)    public String getDetails(@PathVariable(value=&quot;id&quot;) String id,\r\n    @RequestParam(value=&quot;param1&quot;, required=true) String param1,\r\n    @RequestParam(value=&quot;param2&quot;, required=false) String param2){\r\n.......\r\n}\r\n\r\n@GetMapping(&quot;\/user\/{firstName}\/{lastName}&quot;)\r\n   @ResponseBody\r\n   public String handler(@MatrixVariable(&quot;firstName&quot;) String firstName,\r\n         @MatrixVariable(&quot;lastName&quot;) String lastName\r\n         ) {\r\n\r\n      return &quot;&lt;br&gt;Matxrix variable &lt;br&gt; &quot;\r\n            + &quot;firstName =&quot; + firstName +&quot;&lt;br&gt;&quot;\r\n            + &quot;lastName =&quot; + lastName;\r\n   }\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">@PathVariable is to obtain some placeholder from the URI<br \/>\n@MatrixVariable &#8211; a name-value pair within a path segment is referred as matrix variable. Matrix variables can appear in any path segment, each variable separated with a semicolon (;) and multiple values are separated by comma (,)<br \/>\ni.e.<br \/>\nhttp:\/\/www.example.com\/employee\/Mike;salary=45000;dept=HR<br \/>\nhttp:\/\/www.example.com\/car\/Audi;color=RED,BLACK,WHITE<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>@RequestHeader<\/td>\n<td>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Controller\r\npublic class HelloController {\r\n @RequestMapping(value = &quot;\/hello.htm&quot;)\r\n public String hello(\r\n   @RequestHeader(value=&quot;Accept&quot;) String accept,\r\n   @RequestHeader(value=&quot;Accept-Language&quot;) String acceptLanguage,\r\n   @RequestHeader(value=&quot;User-Agent&quot;, defaultValue=&quot;foo&quot;) String userAgent,\r\n   HttpServletResponse response) {\r\n\r\n  System.out.println(&quot;accept: &quot; + accept);\r\n  System.out.println(&quot;acceptLanguage: &quot; + acceptLanguage);\r\n  System.out.println(&quot;userAgent: &quot; + userAgent);\r\n  \r\n  return null;\r\n }\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\">Reading http requestheader is written in HelloController<br \/>\nThe advantage of using Spring @RequestHeader is that it will automatically throw an exception like HTTP Status 400 &#8211; Missing request header &#8216;X&#8217; for method parameter of type, if the header is NOT sent in the input request (by setting required=true)<\/p>\n<p>@RequestHeader for facilitating use to get the header details easily in our controller class<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Annotation Usage @RequestMapping @RequestMapping(value = &quot;\/{name}&quot;, method = RequestMethod.GET, consumes=&quot;application\/json&quot; produces =&quot;application\/json&quot;, headers={&quot;name=pankaj&quot;, &quot;id=1&quot;}) path (or) (or) name (or) and value: which URL the method is mapped to method: compatible HTTP methods params: filters requests based on presence, absence, or value of HTTP parameters headers: filters requests based on presence, absence, or value of HTTP&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-rest-annotations\/\">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-3175","post","type-post","status-publish","format-standard","hentry","category-rest"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3175","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=3175"}],"version-history":[{"count":9,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3175\/revisions"}],"predecessor-version":[{"id":3227,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3175\/revisions\/3227"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}