In this section we build a consumer webservice application.
We will build an application that uses spring's resttemplate to get data from Http://gturnquist-quoters.cfapps.io/api/random.
Http://gturnquist-quoters.cfapps.io/api/random is a restfulservice interface that randomly references something about springboot and returns it in JSON form.
The JSON format is roughly the following
{
Type: "Success",
Value: {
Id:10,
Quote: "Really loving spring Boot, makes stand alone spring apps easy."
}
}
It is very simple, but usually does not get this kind of data through the browser, but is consumed programmatically.
The template class Resttemplate provided by spring can easily help you with these tasks.
Resttemplate can not only makes interacting with the most RESTful services a one-line incantation, but can also bind data to the client's corresponding entity class.
as follows, create an entity class based on JSON
As you can see, this is simply a Java class that contains some properties and corresponding getter, setter methods.
The purpose of @JsonIgnoreProperties annotations is to ignore some properties when they cannot be bound.
In order for you to bind the data directly to the entity class, you need to specify the attribute name of the entity class to match the key in the JSON, and if it is inconsistent, you need to add Jsonproperty for the entity class attribute to specify the corresponding JSON key.
Code that complements the value class
The main function is as follows
Run as follows
Consumption of Spring guide 03:restful Services