Consumption of Spring guide 03:restful Services

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.