SpringBoot使用RESTful接口詳解
REST簡介
REST(Representational State Transfer 表現(xiàn)層狀態(tài)轉化)是一種軟件架構風格,它是一種針對網(wǎng)絡應用的設計和開發(fā)方法,可以降低開發(fā)的復雜性。提供系統(tǒng)的可伸縮性。
REST是一組架構約束條件和原則 這些約束有
1:使用客戶/服務器模型 客戶和服務器之間通過一個統(tǒng)一的接口來互相通信
2:層次化的系統(tǒng) 在一個REST系統(tǒng)中 服務端并不會固定地與一個服務器打交道
3:無狀態(tài) 服務端并不會保存有關客戶的任何信息,客戶端負責自身狀態(tài)的維持
4:可緩存 REST系統(tǒng)需要適當?shù)木彺嬲埱?減少服務端和客戶端之間的信息傳輸
5:統(tǒng)一的接口 一個REST系統(tǒng)需要一個統(tǒng)一的接口來完成子系統(tǒng)之間以及服務與用戶之間的交互
滿足上述約束條件和原則的應用程序或者設計就是RESTful
一、Spring Boot整合REST
在Spring Boot的Web應用中 自動支持REST 也就是說 只要spring-boot-starter-web依賴在pom.xml文件中 就支持REST
下面通過一個RESTful應用示例來講解
假如在控制器類有如下處理方法
@RequestMapping("/findArticalByAuthor_id/{id}") public List<Article>findByAuthor_id(@PathVariable("id")Integer id){ return authorAndArticleService.findByAuthor_id(id); }
那么可以使用如下所示的REST風格的URL訪問上述處理方法
http://localhost:8080/ch6_2/findArticleByAuthor_id/2
二、Spring Data REST
在Spring Boot應用中使用Spring Data REST只需引入spring-boot-starter-data-rest的依賴即可
下面通過一個實例講解Spring Data REST的構建過程
1:修改pom.xml文件 添加MYSQL依賴
<?xml version="1.0" encoding="UTF-8"?> -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> -<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ch</groupId> <artifactId>ch6_7</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ch6_7</name> <description>Demo project for Spring Boot</description> -<properties> <java.version>11</java.version> </properties> -<dependencies> -<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> -<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <!-- 添加MySQL依賴 --> -<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> <!-- MySQL8.x時,請使用8.x的連接器 --> </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> </plugin> </plugins> </build> </project>
2:設置上下文路徑以及數(shù)據(jù)源配置信息
server.servlet.context-path=/api
###
##數(shù)據(jù)源信息配置
###
#數(shù)據(jù)庫地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#數(shù)據(jù)庫用戶名
spring.datasource.username=root
#數(shù)據(jù)庫密碼
spring.datasource.password=root
#數(shù)據(jù)庫驅動
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
####
#JPA持久化配置
####
#指定數(shù)據(jù)庫類型
spring.jpa.database=MYSQL
#指定是否在日志中顯示SQL語句
spring.jpa.show-sql=true
#指定自動創(chuàng)建、更新數(shù)據(jù)庫表等配置,update表示如果數(shù)據(jù)庫中存在持久化類對應的表就不創(chuàng)建,不存在就創(chuàng)建對應的表
spring.jpa.hibernate.ddl-auto=update
#讓控制器輸出的JSON字符串格式更美觀
spring.jackson.serialization.indent-output=true
3:創(chuàng)建持久化實體類Student
部分代碼如下 省略部分set和get方法
package com.ch.ch6_7.entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "student_table") public class Student implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id;//主鍵 private String sno; private String sname; private String ssex; public Student() { super(); } public Student(int id, String sno, String sname, String ssex) { super(); this.id = id; this.sno = sno; this.sname = sname; this.ssex = ssex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSno() { return sno; } public void setSno(String sno) { ex; } }
4:創(chuàng)建數(shù)據(jù)訪問層
package com.ch.ch6_7.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RestResource; import com.ch.ch6_7.entity.Student; public interface StudentRepository extends JpaRepository<Student, Integer>{ /** * 自定義接口查詢方法,暴露為REST資源 */ @RestResource(path = "snameStartsWith", rel = "snameStartsWith") List<Student> findBySnameStartsWith(@Param("sname") String sname); }
在上述數(shù)據(jù)訪問接口中 使用@RestResource注解將該方法暴露為REST資源
至此 基于Spring Data的REST資源服務已經(jīng)構建完畢 接下來就是使用REST客戶端測試服務
三、REST服務測試
在Web和移動端開發(fā)時,常常會調用服務器端的RESTful的接口進行數(shù)據(jù)請求,為了調試,一般會先用工具進行測試,通過測試后才開始在開發(fā)中使用
Wisdom REST Client是用Java語言編寫的REST客戶端,是Github上的開源項目,可以通過http://github.com/Wisdom-Projects/rest-client地址下載
到此這篇關于SpringBoot使用RESTful接口詳解的文章就介紹到這了,更多相關SpringBoot RESTful接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot重啟后,第一次請求接口請求慢的問題及解決
這篇文章主要介紹了SpringBoot重啟后,第一次請求接口請求慢的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Mybatis?在?insert?插入操作后返回主鍵?id的操作方法
這篇文章主要介紹了Mybatis?在?insert?插入操作后返回主鍵?id的操作方法,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12使用Spring @DependsOn控制bean加載順序的實例
這篇文章主要介紹了使用Spring @DependsOn控制bean加載順序的實例講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07jeefast和Mybatis實現(xiàn)三級聯(lián)動的示例代碼
這篇文章主要介紹了jeefast和Mybatis實現(xiàn)三級聯(lián)動的示例代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Java如何使用ReentrantLock實現(xiàn)長輪詢
這篇文章主要介紹了如何使用ReentrantLock實現(xiàn)長輪詢,對ReentrantLock感興趣的同學,可以參考下2021-04-04