SpringBoot使用RESTful接口詳解
REST簡(jiǎn)介
REST(Representational State Transfer 表現(xiàn)層狀態(tài)轉(zhuǎn)化)是一種軟件架構(gòu)風(fēng)格,它是一種針對(duì)網(wǎng)絡(luò)應(yīng)用的設(shè)計(jì)和開發(fā)方法,可以降低開發(fā)的復(fù)雜性。提供系統(tǒng)的可伸縮性。
REST是一組架構(gòu)約束條件和原則 這些約束有
1:使用客戶/服務(wù)器模型 客戶和服務(wù)器之間通過一個(gè)統(tǒng)一的接口來互相通信
2:層次化的系統(tǒng) 在一個(gè)REST系統(tǒng)中 服務(wù)端并不會(huì)固定地與一個(gè)服務(wù)器打交道
3:無狀態(tài) 服務(wù)端并不會(huì)保存有關(guān)客戶的任何信息,客戶端負(fù)責(zé)自身狀態(tài)的維持
4:可緩存 REST系統(tǒng)需要適當(dāng)?shù)木彺嬲?qǐng)求 減少服務(wù)端和客戶端之間的信息傳輸
5:統(tǒng)一的接口 一個(gè)REST系統(tǒng)需要一個(gè)統(tǒng)一的接口來完成子系統(tǒng)之間以及服務(wù)與用戶之間的交互
滿足上述約束條件和原則的應(yīng)用程序或者設(shè)計(jì)就是RESTful
一、Spring Boot整合REST
在Spring Boot的Web應(yīng)用中 自動(dòng)支持REST 也就是說 只要spring-boot-starter-web依賴在pom.xml文件中 就支持REST
下面通過一個(gè)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的依賴即可
下面通過一個(gè)實(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時(shí),請(qǐng)使用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ù)庫(kù)地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#數(shù)據(jù)庫(kù)用戶名
spring.datasource.username=root
#數(shù)據(jù)庫(kù)密碼
spring.datasource.password=root
#數(shù)據(jù)庫(kù)驅(qū)動(dòng)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
####
#JPA持久化配置
####
#指定數(shù)據(jù)庫(kù)類型
spring.jpa.database=MYSQL
#指定是否在日志中顯示SQL語句
spring.jpa.show-sql=true
#指定自動(dòng)創(chuàng)建、更新數(shù)據(jù)庫(kù)表等配置,update表示如果數(shù)據(jù)庫(kù)中存在持久化類對(duì)應(yīng)的表就不創(chuàng)建,不存在就創(chuàng)建對(duì)應(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客戶端測(cè)試服務(wù)
三、REST服務(wù)測(cè)試
在Web和移動(dòng)端開發(fā)時(shí),常常會(huì)調(diào)用服務(wù)器端的RESTful的接口進(jìn)行數(shù)據(jù)請(qǐng)求,為了調(diào)試,一般會(huì)先用工具進(jìn)行測(cè)試,通過測(cè)試后才開始在開發(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot重啟后,第一次請(qǐng)求接口請(qǐng)求慢的問題及解決
這篇文章主要介紹了SpringBoot重啟后,第一次請(qǐng)求接口請(qǐng)求慢的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Mybatis?在?insert?插入操作后返回主鍵?id的操作方法
這篇文章主要介紹了Mybatis?在?insert?插入操作后返回主鍵?id的操作方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
Java并發(fā)之異步的八種實(shí)現(xiàn)方式
本文主要介紹了Java并發(fā)之異步的八種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
使用Spring @DependsOn控制bean加載順序的實(shí)例
這篇文章主要介紹了使用Spring @DependsOn控制bean加載順序的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
jeefast和Mybatis實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的示例代碼
這篇文章主要介紹了jeefast和Mybatis實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Java如何使用ReentrantLock實(shí)現(xiàn)長(zhǎng)輪詢
這篇文章主要介紹了如何使用ReentrantLock實(shí)現(xiàn)長(zhǎng)輪詢,對(duì)ReentrantLock感興趣的同學(xué),可以參考下2021-04-04

