# gs-consuming-rest **Repository Path**: androidhenry/gs-consuming-rest ## Basic Information - **Project Name**: gs-consuming-rest - **Description**: No description available - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-08-02 - **Last Updated**: 2021-08-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README :spring_boot_version: 2.4.1 :RestTemplate: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html :HttpMessageConverter: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/HttpMessageConverter.html :toc: :icons: font :source-highlighter: prettify :project_id: gs-consuming-rest This guide walks you through the process of creating an application that consumes a RESTful web service. == What You Will Build You will build an application that uses Spring's `RestTemplate` to retrieve a random Spring Boot quotation at https://quoters.apps.pcfone.io/api/random. == What You Need :java_version: 1.8 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/prereq_editor_jdk_buildtools.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/how_to_complete_this_guide.adoc[] [[scratch]] == Starting with Spring Initializr If you use Maven, visit the https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=consuming-rest&name=consuming-rest&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.consuming-rest&dependencies=web[Spring Initializr] to generate a new project with the required dependency (Spring Web). The following listing shows the `pom.xml` file created when you choose Maven: ==== [source,xml] ---- include::initial/pom.xml[] ---- ==== If you use Gradle, visit the https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=consuming-rest&name=consuming-rest&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.consuming-rest&dependencies=web[Spring Initializr] to generate a new project with the required dependency (Spring Web). The following listing shows the `build.gradle` file created when you choose Gradle: ==== [source,gradle] ---- include::initial/build.gradle[] ---- ==== These build files can be this simple because `spring-boot-starter-web` includes everything you need to build a web application, including the Jackson classes you need to work with JSON. === Manual Initialization (optional) If you want to initialize the project manually rather than use the links shown earlier, follow the steps given below: . Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you. . Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java. . Click *Dependencies* and select *Spring Web*. . Click *Generate*. . Download the resulting ZIP file, which is an archive of a web application that is configured with your choices. NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE. [[initial]] == Fetching a REST Resource With project setup complete, you can create a simple application that consumes a RESTful service. A RESTful service has been stood up at https://quoters.apps.pcfone.io/api/random. It randomly fetches quotations about Spring Boot and returns them as JSON documents. If you request that URL through a web browser or curl, you receive a JSON document that looks something like this: ==== [source,java,tabsize=2script] ---- { type: "success", value: { id: 10, quote: "Really loving Spring Boot, makes stand alone Spring apps easy." } } ---- ==== That is easy enough but not terribly useful when fetched through a browser or through curl. A more useful way to consume a REST web service is programmatically. To help you with that task, Spring provides a convenient template class called {RestTemplate}[`RestTemplate`]. `RestTemplate` makes interacting with most RESTful services a one-line incantation. And it can even bind that data to custom domain types. First, you need to create a domain class to contain the data that you need. The following listing shows the `Quote` class, which you can use as your domain class: `src/main/java/com/example/consumingrest/Quote.java` [source,java,tabsize=2] ---- include::complete/src/main/java/com/example/consumingrest/Quote.java[] ---- This simple Java class has a handful of properties and matching getter methods. It is annotated with `@JsonIgnoreProperties` from the Jackson JSON processing library to indicate that any properties not bound in this type should be ignored. To directly bind your data to your custom types, you need to specify the variable name to be exactly the same as the key in the JSON document returned from the API. In case your variable name and key in JSON doc do not match, you can use `@JsonProperty` annotation to specify the exact key of the JSON document. (This example matches each variable name to a JSON key, so you do not need that annotation here.) You also need an additional class, to embed the inner quotation itself. The `Value` class fills that need and is shown in the following listing (at `src/main/java/com/example/consumingrest/Value.java`): ==== [source,java,tabsize=2] ---- include::complete/src/main/java/com/example/consumingrest/Value.java[] ---- ==== This uses the same annotations but maps onto other data fields. == Finishing the Application The Initalizr creates a class with a `main()` method. The following listing shows the class the Initializr creates (at `src/main/java/com/example/consumingrest/ConsumingRestApplication.java`): ==== [source,java] ---- include::initial/src/main/java/com/example/consumingrest/ConsumingRestApplication.java[] ---- ==== Now you need to add a few other things to the `ConsumingRestApplication` class to get it to show quotations from our RESTful source. You need to add: * A logger, to send output to the log (the console, in this example). * A `RestTemplate`, which uses the Jackson JSON processing library to process the incoming data. * A `CommandLineRunner` that runs the `RestTemplate` (and, consequently, fetches our quotation) on startup. The following listing shows the finished `ConsumingRestApplication` class (at `src/main/java/com/example/consumingrest/ConsumingRestApplication.java`): ==== [source,java] ---- include::complete/src/main/java/com/example/consumingrest/ConsumingRestApplication.java[] ---- ==== == Running the Application include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/build_an_executable_jar_with_both.adoc[] You should see output similar to the following but with a random quotation: .... 2019-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.'}} .... NOTE: If you see an error that reads, `Could not extract response: no suitable HttpMessageConverter found for response type [class com.example.consumingrest.Quote]`, it is possible that you are in an environment that cannot connect to the backend service (which sends JSON if you can reach it). Maybe you are behind a corporate proxy. Try setting the `http.proxyHost` and `http.proxyPort` system properties to values appropriate for your environment. == Summary Congratulations! You have just developed a simple REST client by using Spring Boot. == See Also The following guides may also be helpful: * https://spring.io/guides/gs/rest-service/[Building a RESTful Web Service] * https://spring.io/guides/gs/consuming-rest-angularjs/[Consuming a RESTful Web Service with AngularJS] * https://spring.io/guides/gs/consuming-rest-jquery/[Consuming a RESTful Web Service with jQuery] * https://spring.io/guides/gs/consuming-rest-restjs/[Consuming a RESTful Web Service with rest.js] * https://spring.io/guides/gs/accessing-gemfire-data-rest/[Accessing GemFire Data with REST] * https://spring.io/guides/gs/accessing-mongodb-data-rest/[Accessing MongoDB Data with REST] * https://spring.io/guides/gs/accessing-data-mysql/[Accessing data with MySQL] * https://spring.io/guides/gs/accessing-data-rest/[Accessing JPA Data with REST] * https://spring.io/guides/gs/accessing-neo4j-data-rest/[Accessing Neo4j Data with REST] * https://spring.io/guides/gs/securing-web/[Securing a Web Application] * https://spring.io/guides/gs/spring-boot/[Building an Application with Spring Boot] * https://spring.io/guides/gs/testing-restdocs/[Creating API Documentation with Restdocs] * https://spring.io/guides/gs/rest-service-cors/[Enabling Cross Origin Requests for a RESTful Web Service] * https://spring.io/guides/gs/rest-hateoas/[Building a Hypermedia-Driven RESTful Web Service] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/main/footer.adoc[]