SpringBoot实战(三)之使用RestFul Web服务

前端之家收集整理的这篇文章主要介绍了SpringBoot实战(三)之使用RestFul Web服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、导入maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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</>

    groupId>org.springframeworkartifactId>gs-consuming-restversion>0.1.0parent>
        >org.springframework.boot>spring-boot-starter-parent>1.5.8.RELEASEpropertiesjava.version>1.8java.versiondependenciesdependency>
            >spring-boot-starter>spring-web>com.fasterxml.jackson.core>jackson-databind>


    buildpluginsplugin>
                >spring-boot-maven-plugin>

project>

 

二、构建实体

Quote.java

package hello;



import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {

    private String type;
     Value value;

    public Quote() {
    }

     String getType() {
        return type;
    }

    void setType(String type) {
        this.type = Value getValue() {
         value;
    }

     setValue(Value value) {
        this.value = value;
    }

    @Override
     String toString() {
        return "Quote{" +
                "type='" + type + '\'' +
                ",value=" + value +
                '}';
    }
}

 

Value.java

 hello;

 Value {

     Long id;
     String quote;

     Value() {
    }

     Long getId() {
        return this.id;
    }

     String getQuote() {
        .quote;
    }

     setId(Long id) {
        this.id = id;
    }

     setQuote(String quote) {
        this.quote = quote;
    }

    @Override
    return "Value{" +
                "id=" + id +
                ",quote='" + quote + '\'' +
                '}';
    }
}

 

@JsonIgnore注解用来忽略某些字段,可以用在Field或者Getter方法上,用在Setter方法时,和Filed效果一样。这个注解只能用在POJO存在的字段要忽略的情况,不能满足现在需要的情况。

@JsonIgnoreProperties(ignoreUnknown = true),将这个注解写在类上之后,就会忽略类中不存在的字段,可以满足当前的需要。这个注解还可以指定要忽略的字段。使用方法如下:

@JsonIgnoreProperties({ "internalId","secretKey" })
指定的字段不会被序列化和反序列化。

 

 

三、编写启动类

 org.slf4j.Logger;
 org.slf4j.LoggerFactory;

 org.springframework.web.client.RestTemplate;

 Application {

    private static final Logger log = LoggerFactory.getLogger(Application.);

     main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
        Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random",Quote.);
        log.info(quote.toString());
    }

}

 

 

运行后控制台显示结果为:

 

 org.slf4j.LoggerFactory;
 org.springframework.boot.CommandLineRunner;
 org.springframework.boot.SpringApplication;
 org.springframework.boot.autoconfigure.SpringBootApplication;
 org.springframework.boot.web.client.RestTemplateBuilder;
 org.springframework.context.annotation.Bean;
 org.springframework.web.client.RestTemplate;

@SpringBootApplication
 main(String args[]) {
        SpringApplication.run(Application.);
    }

    @Bean
     RestTemplate restTemplate(RestTemplateBuilder builder) {
         builder.build();
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            Quote quote = restTemplate.getForObject(
                    "http://gturnquist-quoters.cfapps.io/api/random",1)">);
            log.info(quote.toString());
        };
    }
}

RestTemplateBuilder是由Spring注入的,如果你使用它来创建一个,RestTemplate那么你将受益于Spring Boot中带有消息转换器和请求工厂的所有自动配置。我们还将其提取RestTemplate为a @Bean以使其更容易测试(可以通过这种方式更容易地进行模拟)。

 

后执行mvn clean install或者mvn install,生成jar包,通过cmd命令执行java -jar  gs-consume-rest-0.1.0.jar

出现如红色标记处,表示成功开发一个RestFul客户端。

猜你在找的Springboot相关文章