亚洲欧美日韩熟女|做爱高潮视频网址|国产一区二区三级片|国产Av中文字幕www.性色av|亚洲婷婷永久免费|国产高清中文字幕|欧美变态网站久re视频精品|人妻AV鲁丝第一页|天堂AV一区二区在线观看|综合 91在线精品

springboot學(xué)習(xí)之如何調(diào)用RESTful Web服務(wù)

2023-04-12


springboot學(xué)習(xí)之如何調(diào)用RESTful Web服務(wù)

  • 新建工程gs-consuming-rest
  • 新建entity
  • 完善Application
  • 模擬遠程服務(wù)
  • 運行g(shù)s-consuming-rest工程
  • 學(xué)習(xí)總結(jié)


此文意在學(xué)習(xí)創(chuàng)建一個工程并調(diào)用一個遠程服務(wù)。


新建工程gs-consuming-rest


新建工程步驟參考:《springboot學(xué)習(xí)之構(gòu)建 RESTful Web服務(wù)》新建web 工程。


新建entity

public class Greeting {

    private long id;
    private String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Greeting{" +
                "id=" + id +
                ", content='" + content + '\'' +
                '}';
    }
}

完善Application

@SpringBootApplication
public class ConsumingRestApplication {

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

    public static void main(String[] args) {
        SpringApplication.run(ConsumingRestApplication.class, args);
    }

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

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            Greeting greeting = restTemplate.getForObject(
                    "http://127.0.0.1:8080/gs-rest-service/greeting", Greeting.class);
            log.info(greeting.toString());
        };
    }

}

CommandLineRunner表示在啟動時執(zhí)行RestTemplate


模擬遠程服務(wù)


使用gs-rest-service服務(wù)實例,該服務(wù)提供一個接口:http://127.0.0.1:8080/gs-rest-service/greeting



在瀏覽器訪問兩次,第二次返回數(shù)據(jù)如下:{“id”:2,“content”:“Hello, World!”}


運行g(shù)s-consuming-rest工程




可以看到上圖中正常返回數(shù)據(jù)為:{“id”:3,“content”:“Hello, World!”}


學(xué)習(xí)總結(jié)


主要學(xué)習(xí)了使用Spring Boot 開發(fā)一個簡單REST客戶端。




本文僅代表作者觀點,版權(quán)歸原創(chuàng)者所有,如需轉(zhuǎn)載請在文中注明來源及作者名字。

免責(zé)聲明:本文系轉(zhuǎn)載編輯文章,僅作分享之用。如分享內(nèi)容、圖片侵犯到您的版權(quán)或非授權(quán)發(fā)布,請及時與我們聯(lián)系進行審核處理或刪除,您可以發(fā)送材料至郵箱:service@tojoy.com