{"id":21656826,"url":"https://github.com/sharanggupta/gs-consuming-rest","last_synced_at":"2026-05-07T15:31:19.983Z","repository":{"id":264096568,"uuid":"889257195","full_name":"sharanggupta/gs-consuming-rest","owner":"sharanggupta","description":"Learn how to retrieve web page data with Spring's RestTemplate.","archived":false,"fork":false,"pushed_at":"2024-11-15T23:29:31.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-29T21:48:27.633Z","etag":null,"topics":["resttemplate","resttemplate-configuration","spring-boot"],"latest_commit_sha":null,"homepage":"https://spring.io/guides/gs/consuming-rest/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sharanggupta.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-15T23:20:01.000Z","updated_at":"2024-11-15T23:29:35.000Z","dependencies_parsed_at":"2024-11-22T01:26:45.510Z","dependency_job_id":"d0caf9ce-f8a2-4147-a34e-075c38d8918e","html_url":"https://github.com/sharanggupta/gs-consuming-rest","commit_stats":null,"previous_names":["sharanggupta/gs-consuming-rest"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sharanggupta/gs-consuming-rest","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharanggupta%2Fgs-consuming-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharanggupta%2Fgs-consuming-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharanggupta%2Fgs-consuming-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharanggupta%2Fgs-consuming-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sharanggupta","download_url":"https://codeload.github.com/sharanggupta/gs-consuming-rest/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sharanggupta%2Fgs-consuming-rest/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32743910,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-07T02:14:30.463Z","status":"ssl_error","status_checked_at":"2026-05-07T02:14:29.405Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["resttemplate","resttemplate-configuration","spring-boot"],"created_at":"2024-11-25T09:17:44.988Z","updated_at":"2026-05-07T15:31:19.961Z","avatar_url":"https://github.com/sharanggupta.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":":RestTemplate: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html\n:toc:\n:icons: font\n:source-highlighter: prettify\n:project_id: gs-consuming-rest\n\nThis guide walks you through the process of creating an application that consumes a\nRESTful web service.\n\n== What You Will Build\n\nYou will build an application that uses Spring's `RestTemplate` to retrieve a random\nSpring Boot quotation at http://localhost:8080/api/random.\n\n== What You Need\n\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/prereq_editor_jdk_buildtools.adoc[]\n\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/how_to_complete_this_guide.adoc[]\n\n[[scratch]]\n== Starting with Spring Initializr\n\nYou can use this https://start.spring.io/#!type=maven-project\u0026groupId=com.example\u0026artifactId=consuming-rest\u0026name=consuming-rest\u0026description=Demo%20project%20for%20Spring%20Boot\u0026packageName=com.example.consuming-rest\u0026dependencies=web[pre-initialized project] and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.\n\nTo manually initialize the project:\n\n. Navigate to https://start.spring.io.\nThis service pulls in all the dependencies you need for an application and does most of the setup for you.\n. Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.\n. Click *Dependencies* and select *Spring Web*.\n. Click *Generate*.\n. Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.\n\nNOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.\n\nNOTE: You can also fork the project from Github and open it in your IDE or other editor.\n\n[[initial]]\n== Fetching a REST Resource\n\nWith project setup complete, you can create a simple application that consumes a RESTful\nservice.\n\nBefore you can do so, you need a source of REST resources.\nWe have provided an example of such a service at https://github.com/spring-guides/quoters.\nYou can run that application in a separate terminal and access the result at http://localhost:8080/api/random.\nThat address randomly fetches a quotation about Spring Boot and returns it as a JSON document.\nOther valid addresses include http://localhost:8080/api/ (for all the quotations)\nand http://localhost:8080/api/1 (for the first quotation), http://localhost:8080/api/2\n(for the second quotation), and so on (up to 10 at present).\n\nIf you request that URL through a web browser or curl, you receive a JSON document\nthat looks something like this:\n\n====\n[source,java,tabsize=2script]\n----\n{\n   type: \"success\",\n   value: {\n      id: 10,\n      quote: \"Really loving Spring Boot, makes stand alone Spring apps easy.\"\n   }\n}\n----\n====\n\nThat is easy enough but not terribly useful when fetched through a browser or through curl.\n\nA more useful way to consume a REST web service is programmatically. To help you with that\ntask, Spring provides a convenient template class called {RestTemplate}[`RestTemplate`].\n`RestTemplate` makes interacting with most RESTful services a one-line incantation. And it\ncan even bind that data to custom domain types.\n\nFirst, you need to create a domain class to contain the data that you need. The following\nlisting shows the `Quote` record class, which you can use as your domain class:\n\n`src/main/java/dev/sharanggupta/gs_consuming_rest/Quote.java`\n[source,java,tabsize=2]\n----\ninclude::src/main/java/dev/sharanggupta/gs_consuming_rest/Quote.java[]\n----\n\nThis simple Java record class is annotated with `@JsonIgnoreProperties` from the Jackson JSON\nprocessing library to indicate that any properties not bound in this type should be ignored.\n\nTo directly bind your data to your custom types, you need to specify the\nvariable name to be exactly the same as the key in the JSON document returned from the API.\nIn case your variable name and key in JSON doc do not match, you can use `@JsonProperty`\nannotation to specify the exact key of the JSON document. (This example matches each\nvariable name to a JSON key, so you do not need that annotation here.)\n\nYou also need an additional class, to embed the inner quotation itself. The `Value` record\nclass fills that need and is shown in the following listing (at\n`src/main/java/dev/sharanggupta/gs_consuming_rest/Value.java`):\n\n====\n[source,java,tabsize=2]\n----\ninclude::src/main/java/dev/sharanggupta/gs_consuming_rest/Value.java[]\n----\n====\n\nThis uses the same annotations but maps onto other data fields.\n\n== Finishing the Application\n\nThe Initializr creates a class with a `main()` method. The following listing shows the\nclass the Initializr creates (at\n`src/main/java/dev/sharanggupta/gs_consuming_rest/GsConsumingRestApplication.java`):\n\n====\n[source,java]\n----\ninclude::src/main/java/dev/sharanggupta/gs_consuming_rest/GsConsumingRestApplication.java[]\n----\n====\n\nNow you need to add a few other things to the `ConsumingRestApplication` class to get it to\nshow quotations from our RESTful source. You need to add:\n\n* A logger, to send output to the log (the console, in this example).\n* A `RestTemplate`, which uses the Jackson JSON processing library to process the incoming\ndata.\n* A `CommandLineRunner` that runs the `RestTemplate` (and, consequently, fetches our\nquotation) on startup.\n\nThe following listing shows the finished `ConsumingRestApplication` class (at\n`src/main/java/dev/sharanggupta/gs_consuming_rest/GsConsumingRestApplication.java`):\n\n====\n[source,java]\n----\ninclude::src/main/java/dev/sharanggupta/gs_consuming_rest/GsConsumingRestApplication.java[]\n----\n====\n\nFinally, you need to set the server port. The quoters application uses the default\nserver port, 8080, so this application cannot also use the same port. You can set\nthe server port to 8081 by adding the following line to application properties\n(which the Initializr created for you):\n\n----\nserver.port=8081\n----\n\n== Running the Application\n\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_an_executable_jar_with_both.adoc[]\n\nYou should see output similar to the following but with a random quotation:\n\n....\n2019-08-22 14:06:46.506  INFO 42940 --- [           main] c.e.c.ConsumingRestApplication           : Quote{type='success', value=Value{id=1, quote='Working with Spring Boot is like pair-programming with the Spring developers.'}}\n....\n\nNOTE: If you see an error that reads, `Could not extract response: no suitable\nHttpMessageConverter found for response type [class com.example.consumingrest.Quote]`, it\nis possible that you are in an environment that cannot connect to the backend service\n(which sends JSON if you can reach it). Maybe you are behind a corporate proxy. Try\nsetting the `http.proxyHost` and `http.proxyPort` system properties to values appropriate\nfor your environment.\n\n== Summary\nCongratulations! You have just developed a simple REST client by using Spring Boot.\n\n== See Also\n\nThe following guides may also be helpful:\n\n* https://spring.io/guides/gs/rest-service/[Building a RESTful Web Service]\n* https://spring.io/guides/gs/consuming-rest-angularjs/[Consuming a RESTful Web Service with AngularJS]\n* https://spring.io/guides/gs/consuming-rest-jquery/[Consuming a RESTful Web Service with jQuery]\n* https://spring.io/guides/gs/consuming-rest-restjs/[Consuming a RESTful Web Service with rest.js]\n* https://spring.io/guides/gs/accessing-gemfire-data-rest/[Accessing GemFire Data with REST]\n* https://spring.io/guides/gs/accessing-mongodb-data-rest/[Accessing MongoDB Data with REST]\n* https://spring.io/guides/gs/accessing-data-mysql/[Accessing data with MySQL]\n* https://spring.io/guides/gs/accessing-data-rest/[Accessing JPA Data with REST]\n* https://spring.io/guides/gs/accessing-neo4j-data-rest/[Accessing Neo4j Data with REST]\n* https://spring.io/guides/gs/securing-web/[Securing a Web Application]\n* https://spring.io/guides/gs/spring-boot/[Building an Application with Spring Boot]\n* https://spring.io/guides/gs/testing-restdocs/[Creating API Documentation with Restdocs]\n* https://spring.io/guides/gs/rest-service-cors/[Enabling Cross Origin Requests for a RESTful Web Service]\n* https://spring.io/guides/gs/rest-hateoas/[Building a Hypermedia-Driven RESTful Web Service]\n\ninclude::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/footer.adoc[]","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsharanggupta%2Fgs-consuming-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsharanggupta%2Fgs-consuming-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsharanggupta%2Fgs-consuming-rest/lists"}