{"id":3165,"date":"2020-04-11T21:04:18","date_gmt":"2020-04-11T20:04:18","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3165"},"modified":"2020-04-19T09:25:38","modified_gmt":"2020-04-19T08:25:38","slug":"how-to-consume-web-services-in-grails","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/fr\/2020\/04\/11\/how-to-consume-web-services-in-grails\/","title":{"rendered":"How to consume web services in Grails"},"content":{"rendered":"\n<p class=\"has-drop-cap\">Frankly, implementing RESTful API endpoints\/services is a vital part of any modern web-based applications these days because of inherent advantages of this technology. However, how do you consume <a href=\"https:\/\/www.itersdesktop.com\/2015\/10\/11\/what-exactly-is-restful-programming\/\">RESTful services<\/a> if you are a client\/user? There are several approaches to allow us fetch data from servers where they support programmatic access such as using HTTP libraries and requests. <\/p>\n\n\n\n<p>In this post, I am going to present an approach of retrieving API endpoints in <a href=\"https:\/\/grails.org\/\">Grails<\/a> based applications using <a href=\"http:\/\/grails.org\/plugin\/rest-client-builder\">rest-client-builder<\/a> plugin. This plugin provides an alternative REST client implementation based on <a href=\"https:\/\/spring.io\/guides\/gs\/consuming-rest\/\">Spring&rsquo;s RestTemplate<\/a> that is not tied to <a href=\"https:\/\/hc.apache.org\/httpclient-3.x\/\">commons-http-client<\/a>, a library to allow Java developers to drag database from API based endpoints. If you would like to play with http client based route, please consult the <a href=\"https:\/\/hc.apache.org\">alternative solution<\/a> to this no longer maintained library.<\/p>\n\n\n\n<p><strong>Notes<\/strong>: The illustrated and introductory project was built in <a href=\"https:\/\/grails.github.io\/grails2-doc\/2.5.6\/guide\/\">grails 2.5.6<\/a>.<\/p>\n\n\n\n<p>If you read through the plugin documentation, you should know the following line to be added to plugin section of <strong>BuildConfig.groovy<\/strong> file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">compile \":rest-client-builder:2.1.1\"<\/pre>\n\n\n\n<p>The project includes a controller named SearchController and a service called SearchService. To demonstrate how the plugin supports, I implemented a method called <code>retrieveBioModelsAllCuratedModels<\/code> to fetch all manually curated models from <a href=\"https:\/\/www.ebi.ac.uk\/biomodels\">BioModels repository<\/a> which the service was fully supported. If you want to learn more about accessing data from BioModels programmatically, please consult their <a href=\"https:\/\/www.ebi.ac.uk\/biomodels\/dev\">documentation page<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/ITersDesktopBlog.001-1024x576.jpeg\" alt=\"A presentation of retrieving data from BioModels repository\" class=\"wp-image-3172\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/ITersDesktopBlog.001-1024x576.jpeg 1024w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/ITersDesktopBlog.001-300x169.jpeg 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/ITersDesktopBlog.001-768x432.jpeg 768w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/ITersDesktopBlog.001.jpeg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>A presentation of retrieving data from BioModels repository<\/em><\/figcaption><\/figure>\n\n\n\n<p>Below is a sample snippet to retrieve data with GET protocol.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">JSONElement retrieveBioModelsAllCuratedModels() {\n        final String BM_SEARCH_URL = \"https:\/\/www.ebi.ac.uk\/biomodels\/search?domain=biomodels\"\n        String queryURL = \"\"\"\\\n${BM_SEARCH_URL}&amp;query=*:* AND curationstatus:\\\"Manually curated\\\" AND NOT isprivate:true&amp;format=json\"\"\"\n        RestBuilder rest = new RestBuilder(connectTimeout: 10000, readTimeout: 100000, proxy: null)\n        def response = rest.get(queryURL) {\n            accept(\"application\/json\")\n            accept(\"application\/xml\")\n            contentType(\"application\/json;charset=UTF-8\")\n        }\n        if (response.status == 200) {\n            return response.json\n        }\n        return null\n    }<\/pre>\n\n\n\n<p>To display the result returned from BioModels, I called the service above directly in index action although it isn&rsquo;t good practice. I recommend you creating a specific action in SearchController to call business logics implemented in your services. The result as you see in SearchService is a JSONElement so that I just render as it is. Below is a screenshot of the result prettify with <a href=\"http:\/\/jsoneditoronline.org\/\">JSON Editor Online<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"660\" src=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-11-at-20.52.45.png\" alt=\"A screenshot of JSON returned from BioModels\" class=\"wp-image-3167\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-11-at-20.52.45.png 590w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2020\/04\/Screenshot-2020-04-11-at-20.52.45-268x300.png 268w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><figcaption><em>A screenshot of JSON returned from BioModels of the query trying to fetch all manually curated models<\/em><\/figcaption><\/figure>\n\n\n\n<p>Anyone is interesting in using this route to access programmatically data from remote resources, the sample project has been pushed to <a href=\"https:\/\/bitbucket.org\/itersdesktop\/rest-client-builder-demo\/\">bitbucket<\/a>.<\/p>\n\n\n\n<p>Hopefully, these tricks would bring a convenient approach to consume API endpoints in your Grails applications.<\/p>\n\n\n\n<p>All constructive comments are welcomed. If you are willing to contribute financial support for our website, please follow the instructions below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Frankly, implementing RESTful API endpoints\/services is a vital part of any modern web-based applications these days because of inherent advantages of this technology. However, how do you consume RESTful services&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[593,4,222,706],"tags":[696,693,697,708,707,695],"class_list":["post-3165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-grails","category-web-engineering","category-web-programming","category-web-services","tag-biomodels-repository","tag-consume-restful-services","tag-manually-curated-models","tag-rest-client","tag-restful-api","tag-retrieve-restful-api-client"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/comments?post=3165"}],"version-history":[{"count":6,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3165\/revisions"}],"predecessor-version":[{"id":3176,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/posts\/3165\/revisions\/3176"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media\/3172"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/media?parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/categories?post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/fr\/wp-json\/wp\/v2\/tags?post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}