{"id":8615,"date":"2020-11-11T19:07:37","date_gmt":"2020-11-11T19:07:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8615"},"modified":"2020-11-11T19:07:37","modified_gmt":"2020-11-11T19:07:37","slug":"spring-boot-and-cors","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/spring-boot-and-cors\/","title":{"rendered":"Spring boot and CORS"},"content":{"rendered":"<p>A while back I wrote a post <a href=\"https:\/\/putridparrot.com\/blog\/building-a-rest-service-with-spring\/\" rel=\"noopener noreferrer\" target=\"_blank\">Building a REST service with Spring<\/a> and today I needed to try out the CORS support to allow a similar Spring based REST service to allow for CORS access from a React application I&#8217;m working on.<\/p>\n<p>It&#8217;s super easy to allow access to all clients by simply adding @CrossOrigin to your controller, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage demo;\r\n\r\nimport org.springframework.web.bind.annotation.CrossOrigin;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RestController;\r\n\r\n@CrossOrigin\r\n@RestController\r\npublic class SampleController {\r\n\r\n    @RequestMapping(&quot;\/purchaseOrder&quot;)\r\n    public PurchaseOrderType getPurchaseOrder() {\r\n        return new PurchaseOrderType(1234);\r\n    }\r\n}\r\n<\/pre>\n<p>Equally we can just add the same annotation to REST methods instead of the whole controller, for example <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@CrossOrigin\r\n@RequestMapping(&quot;\/purchaseOrder&quot;)\r\npublic PurchaseOrderType getPurchaseOrder() {\r\n   return new PurchaseOrderType(1234);\r\n}\r\n<\/pre>\n<p><em>Note: @CrossOrigin is the equivalent of @CrossOrigin(origins = &#8220;*&#8221;).<\/em><\/p>\n<p>If we want to limit the origins then we simply use the following instead<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@CrossOrigin(origins = &quot;http:\/\/localhost:3000&quot;)\r\n\r\n\/\/ or an array of origins like this\r\n\r\n@CrossOrigin(origins = {&quot;http:\/\/localhost:3000&quot;, &quot;http:\/\/localhost:3001&quot;})\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A while back I wrote a post Building a REST service with Spring and today I needed to try out the CORS support to allow a similar Spring based REST service to allow for CORS access from a React application I&#8217;m working on. It&#8217;s super easy to allow access to all clients by simply adding [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[311,161,238],"tags":[],"class_list":["post-8615","post","type-post","status-publish","format-standard","hentry","category-cors","category-java","category-spring-boot"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8615"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8615\/revisions"}],"predecessor-version":[{"id":8619,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8615\/revisions\/8619"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}