SpringBoot使用RESTful接口詳解
REST簡介
REST(Representational State Transfer 表現(xiàn)層狀態(tài)轉化)是一種軟件架構風格,它是一種針對網絡應用的設計和開發(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ù)據源配置信息
server.servlet.context-path=/api
###
##數(shù)據源信息配置
###
#數(shù)據庫地址
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
#數(shù)據庫用戶名
spring.datasource.username=root
#數(shù)據庫密碼
spring.datasource.password=root
#數(shù)據庫驅動
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
####
#JPA持久化配置
####
#指定數(shù)據庫類型
spring.jpa.database=MYSQL
#指定是否在日志中顯示SQL語句
spring.jpa.show-sql=true
#指定自動創(chuàng)建、更新數(shù)據庫表等配置,update表示如果數(shù)據庫中存在持久化類對應的表就不創(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ù)據訪問層
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ù)據訪問接口中 使用@RestResource注解將該方法暴露為REST資源
至此 基于Spring Data的REST資源服務已經構建完畢 接下來就是使用REST客戶端測試服務
三、REST服務測試
在Web和移動端開發(fā)時,常常會調用服務器端的RESTful的接口進行數(shù)據請求,為了調試,一般會先用工具進行測試,通過測試后才開始在開發(fā)中使用
Wisdom REST Client是用Java語言編寫的REST客戶端,是Github上的開源項目,可以通過http://github.com/Wisdom-Projects/rest-client地址下載
到此這篇關于SpringBoot使用RESTful接口詳解的文章就介紹到這了,更多相關SpringBoot RESTful接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java使用Soap方式調用WebService接口代碼示例
Java調用WebService接口是指通過Java語言來訪問并與WebService進行交互,WebService是一種基于Web的服務架構,它通過標準的XML和HTTP協(xié)議來提供服務,這篇文章主要給大家介紹了關于Java使用Soap方式調用WebService接口的相關資料,需要的朋友可以參考下2024-03-03
SpringBoot中OKHttp和壓縮文件的使用實戰(zhàn)教程
本文介紹了如何在SpringBoot中使用OKHttp發(fā)起請求和處理壓縮文件,包括文件的存儲配置、實體類、配置類和初始化類的設置,以及如何通過主程序和測試類進行實際操作,最后提供了必要的依賴添加方法,以確保功能的實現(xiàn)2024-10-10
java中http請求之restTemplate配置超時時間問題解決
這篇文章主要介紹了java中http請求之restTemplate配置超時時間,本文給大家分享三種解決方法,結合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
SpringBoot在一定時間內限制接口請求次數(shù)的實現(xiàn)示例
在項目中,接口的暴露在外面,很多人就會惡意多次快速請求,本文主要介紹了SpringBoot在一定時間內限制接口請求次數(shù)的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2022-03-03

