日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot文档阅读笔记-构建Restful风格的WebService客户端

發(fā)布時間:2025/3/15 javascript 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot文档阅读笔记-构建Restful风格的WebService客户端 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對應(yīng)的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.it1995</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

這里官方已經(jīng)提供了一個WebService服務(wù)端的地址:

https://gturnquist-quoters.cfapps.io/api/random

他會生成由quotation對象生成的JSON。

如下JSON

{type: "success",value: {id: 10,quote: "Really loving Spring Boot, makes stand alone Spring apps easy."} }

使用RestTemplate與WebService進行交互

創(chuàng)建Quote類用于接收WebService傳來的數(shù)據(jù),Quote代碼如下:

package cn.it1995.demo;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@JsonIgnoreProperties(ignoreUnknown = true) public class Quote {private String type;private Value value;public Quote(){}public String getType() {return type;}public void setType(String type) {this.type = type;}public Value getValue() {return value;}public void setValue(Value value) {this.value = value;}@Overridepublic String toString() {return "Quote{" +"type='" + type + '\'' +", value=" + value +'}';} }

@JsonIgnoreProperties注解來源于Jackson庫,作用是忽略屬性的綁定,如果變量名和傳輸過來的json的名是一樣的,就可以直接綁定,如果不一樣,需要使用@JsonProperty進行明確的指定。

下面是關(guān)于JSON中value對應(yīng)的java類代碼:

package cn.it1995.demo;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;@JsonIgnoreProperties(ignoreUnknown = true) public class Value {private Long id;private String quote;public Value(){}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getQuote() {return quote;}public void setQuote(String quote) {this.quote = quote;}@Overridepublic String toString() {return "Value{" +"id=" + id +", quote='" + quote + '\'' +'}';} }

下面是發(fā)起請求相關(guān)的代碼:

package cn.it1995.demo;import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;@SpringBootApplication public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder){return builder.build();}@Beanpublic CommandLineRunner runner(RestTemplate restTemplate){byte[] b = new byte[]{1, 2};System.out.println(b);return args -> {Quote quote = restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);System.out.println(quote);};} }

RestTemplate使用JackSon的JSON庫將傳輸過來的json轉(zhuǎn)換為對象。編程里面的對象,不是你們的對象,這個要注意。

程序運行截圖如下:

程序打包下載地址:

https://github.com/fengfanchen/Java/tree/master/RestWebServiceClient

總結(jié)

以上是生活随笔為你收集整理的Spring Boot文档阅读笔记-构建Restful风格的WebService客户端的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。