First, import maven dependency
<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupId>Org.springframework</groupId> <Artifactid>Gs-consuming-rest</Artifactid> <version>0.1.0</version> <Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>1.5.8.RELEASE</version> </Parent> <Properties> <Java. Version>1.8</java.version> </Properties> <Dependencies> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-web</Artifactid> </Dependency> <Dependency> <groupId>Com.fasterxml.jackson.core</groupId> <Artifactid>Jackson-databind</Artifactid> </Dependency> </Dependencies> <Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build></Project>
Ii. Building Entities
Quote.java
PackageHello;Importcom.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties (Ignoreunknown=true) Public classQuote {PrivateString type; Privatevalue value; PublicQuote () {} PublicString GetType () {returntype; } Public voidsetType (String type) { This. Type =type; } PublicValue GetValue () {returnvalue; } Public voidSetValue (value value) { This. Value =value; } @Override PublicString toString () {return"quote{" + "type=" + type + ' \ ' + ", value=" + Value + '} '; }}
Value.java
PackageHello;Importcom.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties (Ignoreunknown=true) Public classValue {PrivateLong ID; PrivateString Quote; PublicValue () {} PublicLong getId () {return This. ID; } PublicString getquote () {return This. Quote; } Public voidsetId (Long id) { This. ID =ID; } Public voidsetquote (String quote) { This. Quote =quote; } @Override PublicString toString () {return"value{" + "id=" + ID + ", quote= ' + quote + ' \ ' + '} '; }}
@JsonIgnore annotations are used to ignore certain fields and can be used on field or Getter methods, as in setter methods, as with filed effects. This annotation can only be used when the fields that exist in the Pojo are ignored and do not meet the needs of the current situation.
@JsonIgnoreProperties (Ignoreunknown = True), after the annotation is written on the class, fields that do not exist in the class are ignored, and the current needs are met. This annotation can also specify the fields to ignore. Here's how to use it:
@JsonIgnoreProperties ({"Internalid", "Secretkey"})
The specified field is not serialized and deserialized.
Third, write the startup class
PackageHello;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.web.client.RestTemplate; Public classApplication {Private Static FinalLogger log = Loggerfactory.getlogger (application.class); Public Static voidMain (String args[]) {resttemplate resttemplate=Newresttemplate (); Quote Quote= Resttemplate.getforobject ("Http://gturnquist-quoters.cfapps.io/api/random", Quote.class); Log.info (Quote.tostring ()); }}
After running the console displays the results as:
PackageHello;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.boot.CommandLineRunner;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.web.client.RestTemplateBuilder;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.web.client.RestTemplate; @SpringBootApplication Public classApplication {Private Static FinalLogger log = Loggerfactory.getlogger (application.class); Public Static voidMain (String args[]) {springapplication.run (application.class); } @Bean Publicresttemplate resttemplate (Resttemplatebuilder builder) {returnBuilder.build (); } @Bean PublicCommandlinerunner Run (resttemplate resttemplate)throwsException {returnArgs-{Quote Quote=Resttemplate.getforobject ("Http://gturnquist-quoters.cfapps.io/api/random", Quote.class); Log.info (Quote.tostring ()); }; }}
it RestTemplateBuilder
is injected by spring, if you use it to create a, RestTemplate
then you will benefit from spring boot with all the automatic configuration of the message converter and request factory. We've also extracted it RestTemplate
as a to make it easier to test (it's easier to @Bean
simulate in this way).
Finally execute the mvn clean install or mvn install, generate the jar package, execute the Java-jar through the cmd command Gs-consume-rest-0.1.0.jar
Appears as a red flag indicating successful development of a restful client.
Springboot Combat (iii) Use of RESTful Web services