spring boot教程,spring boot demo( 獲取一個RESTful web service)

 2023-11-19 阅读 33 评论 0

摘要:Consuming a RESTful Web Service spring demo中給的接口是 ?http://gturnquist-quoters.cfapps.io/api/random. 返回的數據(用瀏覽器或者curl命令等): spring boot教程、{ ? ?type: "success", ? ?value: { ? ? ? id: 10, ? ? ? quote: "Re

Consuming a RESTful Web Service

spring demo中給的接口是 ?http://gturnquist-quoters.cfapps.io/api/random.

返回的數據(用瀏覽器或者curl命令等):

spring boot教程、{
? ?type: "success",
? ?value: {
? ? ? id: 10,
? ? ? quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
? ?}
}

Spring提供了一個方便的模板類,叫做RestTemplate。與大多數RestTemplate RESTful服務交互。

下面我們編寫java beans:

spring boot 登錄。

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {private String type;private Value value;}
正如你可以看到,這是一個極少數的屬性和匹配的getter方法簡單的Java類。它有jackson JSON處理庫jsonignoreproperties表明any properties not bound in this type should be ignored.


Spring boot、

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {private Long id;private String quote;}
現在我們用 RestTemplate來編寫application 類


spring boot pom、

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
public class Application implements CommandLineRunner {private static final Logger log = LoggerFactory.getLogger(Application.class);public static void main(String args[]) {SpringApplication.run(Application.class);}@Overridepublic void run(String... strings) throws Exception {RestTemplate restTemplate = new RestTemplate();Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);log.info(quote.toString());}
}
因為Jackson JSON處理庫是在classpath中,RestTemplate將使用它(通過消息轉換器)轉換傳入的JSON數據到一個對象。

從那里,引用對象的內容將被log到控制臺。

springboot注解?

在這里,你使用RestTemplate接受HTTP GET請求。但RestTemplate還支持其他HTTP put post delete。


org.springframework.boot.CommandLineRunner;

接口用來指示SpringApplication包含的bean應在跑的時候執行。多commandlinerunner beans可以在同一應用程序的上下文中定義的,

可以責令使用 Ordered interface 或者 @Order注解。


使用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</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.2.5.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>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? ? <version>1.16.2</version>
? ? ? ? ? ? <scope>provided</scope>
? ? ? ? </dependency>
? ? </dependencies>




? ? <build>
? ? ? ? <plugins>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? </build>


</project>


版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183420.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息