SpringBoot使用RESTful接口詳解
REST簡介
REST(Representational State Transfer 表現(xiàn)層狀態(tài)轉(zhuǎn)化)是一種軟件架構(gòu)風(fēng)格,它是一種針對網(wǎng)絡(luò)應(yīng)用的設(shè)計(jì)和開發(fā)方法,可以降低開發(fā)的復(fù)雜性。提供系統(tǒng)的可伸縮性。
REST是一組架構(gòu)約束條件和原則 這些約束有
1:使用客戶/服務(wù)器模型 客戶和服務(wù)器之間通過一個統(tǒng)一的接口來互相通信
2:層次化的系統(tǒng) 在一個REST系統(tǒng)中 服務(wù)端并不會固定地與一個服務(wù)器打交道
3:無狀態(tài) 服務(wù)端并不會保存有關(guān)客戶的任何信息,客戶端負(fù)責(zé)自身狀態(tài)的維持
4:可緩存 REST系統(tǒng)需要適當(dāng)?shù)木彺嬲埱?減少服務(wù)端和客戶端之間的信息傳輸
5:統(tǒng)一的接口 一個REST系統(tǒng)需要一個統(tǒng)一的接口來完成子系統(tǒng)之間以及服務(wù)與用戶之間的交互
滿足上述約束條件和原則的應(yīng)用程序或者設(shè)計(jì)就是RESTful
一、Spring Boot整合REST
在Spring Boot的Web應(yīng)用中 自動支持REST 也就是說 只要spring-boot-starter-web依賴在pom.xml文件中 就支持REST
下面通過一個RESTful應(yīng)用示例來講解
假如在控制器類有如下處理方法
@RequestMapping("/findArticalByAuthor_id/{id}")
public List<Article>findByAuthor_id(@PathVariable("id")Integer id){
return authorAndArticleService.findByAuthor_id(id);
}那么可以使用如下所示的REST風(fēng)格的URL訪問上述處理方法
http://localhost:8080/ch6_2/findArticleByAuthor_id/2
二、Spring Data REST
在Spring Boot應(yīng)用中使用Spring Data REST只需引入spring-boot-starter-data-rest的依賴即可
下面通過一個實(shí)例講解Spring Data REST的構(gòu)建過程
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è)置上下文路徑以及數(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ù)庫驅(qū)動
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ù)庫中存在持久化類對應(yīng)的表就不創(chuàng)建,不存在就創(chuàng)建對應(yīng)的表
spring.jpa.hibernate.ddl-auto=update
#讓控制器輸出的JSON字符串格式更美觀
spring.jackson.serialization.indent-output=true
3:創(chuàng)建持久化實(shí)體類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資源服務(wù)已經(jīng)構(gòu)建完畢 接下來就是使用REST客戶端測試服務(wù)
三、REST服務(wù)測試
在Web和移動端開發(fā)時,常常會調(diào)用服務(wù)器端的RESTful的接口進(jìn)行數(shù)據(jù)請求,為了調(diào)試,一般會先用工具進(jìn)行測試,通過測試后才開始在開發(fā)中使用
Wisdom REST Client是用Java語言編寫的REST客戶端,是Github上的開源項(xiàng)目,可以通過http://github.com/Wisdom-Projects/rest-client地址下載
到此這篇關(guān)于SpringBoot使用RESTful接口詳解的文章就介紹到這了,更多相關(guān)SpringBoot RESTful接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java BASE64Encoder詳細(xì)介紹及簡單實(shí)例
這篇文章主要介紹了java BASE64Encoder詳細(xì)介紹及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01
Java使用Soap方式調(diào)用WebService接口代碼示例
Java調(diào)用WebService接口是指通過Java語言來訪問并與WebService進(jìn)行交互,WebService是一種基于Web的服務(wù)架構(gòu),它通過標(biāo)準(zhǔn)的XML和HTTP協(xié)議來提供服務(wù),這篇文章主要給大家介紹了關(guān)于Java使用Soap方式調(diào)用WebService接口的相關(guān)資料,需要的朋友可以參考下2024-03-03
SpringBoot中OKHttp和壓縮文件的使用實(shí)戰(zhàn)教程
本文介紹了如何在SpringBoot中使用OKHttp發(fā)起請求和處理壓縮文件,包括文件的存儲配置、實(shí)體類、配置類和初始化類的設(shè)置,以及如何通過主程序和測試類進(jìn)行實(shí)際操作,最后提供了必要的依賴添加方法,以確保功能的實(shí)現(xiàn)2024-10-10
java中http請求之restTemplate配置超時時間問題解決
這篇文章主要介紹了java中http請求之restTemplate配置超時時間,本文給大家分享三種解決方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
SpringBoot在一定時間內(nèi)限制接口請求次數(shù)的實(shí)現(xiàn)示例
在項(xiàng)目中,接口的暴露在外面,很多人就會惡意多次快速請求,本文主要介紹了SpringBoot在一定時間內(nèi)限制接口請求次數(shù)的實(shí)現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2022-03-03

