當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot文档阅读笔记-构建Restful风格的WebService客户端
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot文档阅读笔记-构建Restful风格的WebService客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
對應的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>這里官方已經提供了一個WebService服務端的地址:
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進行交互
創建Quote類用于接收WebService傳來的數據,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進行明確的指定。
下面是關于JSON中value對應的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 + '\'' +'}';} }下面是發起請求相關的代碼:
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轉換為對象。編程里面的對象,不是你們的對象,這個要注意。
程序運行截圖如下:
程序打包下載地址:
https://github.com/fengfanchen/Java/tree/master/RestWebServiceClient
總結
以上是生活随笔為你收集整理的Spring Boot文档阅读笔记-构建Restful风格的WebService客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-连接本地代理服务
- 下一篇: 信息安全工程师笔记-网络安全测评技术与标