柚子快報(bào)邀請(qǐng)碼778899分享:后端 Restful風(fēng)格及實(shí)踐
柚子快報(bào)邀請(qǐng)碼778899分享:后端 Restful風(fēng)格及實(shí)踐
目錄
1、RESTFUL 原則
1.1、資源與URI
URL,URI
資源
1.2、統(tǒng)一資源接口
1.3、資源的表述
1.4、狀態(tài)
有狀態(tài)和無(wú)狀態(tài)
狀態(tài)轉(zhuǎn)移(應(yīng)用狀態(tài)化改變資源狀態(tài)化)
2、restful風(fēng)格接口書(shū)寫(xiě)方式
3、restful風(fēng)格的實(shí)踐
3.1、項(xiàng)目結(jié)構(gòu)
3.2、創(chuàng)建項(xiàng)目
3.3、配置
3.3.1、添加mybatis plugs依賴(lài)
3.3.2、 添加熱部署
?3.3.3、配置application.yml?
3.4、編碼
持久層
數(shù)據(jù)操作層
服務(wù)接口層
服務(wù)層
控制層?
使用postman進(jìn)行測(cè)試
前言:
? ? ? ?restful是一種風(fēng)格,并不是一種 標(biāo)準(zhǔn),使用它可以更好的創(chuàng)建分布式架構(gòu);restful風(fēng)格中使用名詞定位資源,并使用HTTP協(xié)議里的動(dòng)詞(GET、POST、PUT、DELETE)來(lái)實(shí)現(xiàn)資源的增刪改查操作。?
1、RESTFUL 原則
1.1、資源與URI
URL,URI
URI:全稱(chēng)為Uniform Resource Identifier,即統(tǒng)一資源標(biāo)志符
URI是一個(gè)用來(lái)標(biāo)識(shí)抽象或物理資源的緊湊字符串,通過(guò)這個(gè)標(biāo)識(shí)可以訪問(wèn)一個(gè)唯一的資源。
URL:全稱(chēng)為Universal Resource Locator,即統(tǒng)一資源定位符
URL是一種具體的URI,它是URI的一個(gè)子集,它不僅唯一標(biāo)識(shí)資源,而且還提供了定位該資源的信息。
資源
互聯(lián)網(wǎng)中資源多種多樣,我門(mén)所知的視頻,圖片,文字,音頻,html等都是資源的表述,資源通過(guò)URI進(jìn)行唯一標(biāo)識(shí),每一個(gè)URI代表一種資源,所以我們能通過(guò)檢索查詢(xún)到特定資源。
1.2、統(tǒng)一資源接口
? ? ? ? RESTful架構(gòu)應(yīng)該遵循統(tǒng)一接口原則,統(tǒng)一接口包含了一組受限的預(yù)定義的操作,不論什么樣的資源,都是通過(guò)使用相同的接口進(jìn)行資源的訪問(wèn)。接口應(yīng)該使用標(biāo)準(zhǔn)的HTTP方法如GET,PUT和POST,并遵循這些方法的語(yǔ)義。
? ? ? ? 如果按照HTTP方法的語(yǔ)義來(lái)暴露資源,那么接口將會(huì)擁有安全性和冪等性的特性,例如GET和HEAD請(qǐng)求都是安全的, 無(wú)論請(qǐng)求多少次,都不會(huì)改變服務(wù)器狀態(tài)。而GET、HEAD、PUT和DELETE請(qǐng)求都是冪等的,無(wú)論對(duì)資源操作多少次, 結(jié)果總是一樣的,后面的請(qǐng)求并不會(huì)產(chǎn)生比第一次更多的影響。
get-安全且冪等post-不安全不冪等put-不安全且冪等delete-不安全且冪等
1.3、資源的表述
? ? ? ? ?客戶(hù)端獲取的只是資源的表述而已。 資源在外界的具體呈現(xiàn),可以有多種表述(或成為表現(xiàn)、表示)形式,在客戶(hù)端和服務(wù)端之間傳送的也是資源的表述,而不是資源本身。 例如文本資源可以采用html、xml、json等格式,圖片可以使用PNG或JPG展現(xiàn)出來(lái)。
1.4、狀態(tài)
狀態(tài)可分為應(yīng)用狀態(tài)和資源狀態(tài),應(yīng)用狀態(tài)由客戶(hù)端維護(hù),資源狀態(tài)由服務(wù)器端維護(hù)。
有狀態(tài)和無(wú)狀態(tài)
有狀態(tài)
是指狀態(tài)信息是由服務(wù)器端負(fù)責(zé),例如用戶(hù)通過(guò)提交的cookie信息查找對(duì)應(yīng)服務(wù)器端的session信息,由服務(wù)器確定用戶(hù)狀態(tài)(如cookie和session來(lái)維持登錄)。
無(wú)狀態(tài)
當(dāng)請(qǐng)求端提出請(qǐng)求時(shí),狀態(tài)信息有客戶(hù)客戶(hù)端負(fù)責(zé),請(qǐng)求本身包含了響應(yīng)端為相應(yīng)這一請(qǐng)求所需的全部信息關(guān)聯(lián)的用戶(hù)交互操作保留的某種公共信息,例如JWT風(fēng)格,使用token來(lái)維持狀態(tài)信息。
狀態(tài)轉(zhuǎn)移(應(yīng)用狀態(tài)化改變資源狀態(tài)化)
例如打開(kāi)一個(gè)淘寶主頁(yè)面并點(diǎn)擊某個(gè)商品,按照常理它會(huì)跳轉(zhuǎn)到商品詳情頁(yè)面,可實(shí)際上它跳轉(zhuǎn)到了登錄頁(yè)面。
這是因?yàn)榉?wù)器不能識(shí)別客戶(hù)端狀態(tài),當(dāng)?shù)卿浐?,服?wù)器端的session就可以判斷出是哪個(gè)用戶(hù),并響應(yīng)資源。
狀態(tài)轉(zhuǎn)移可以理解為:根據(jù)用戶(hù)端的狀態(tài)改變資源狀態(tài)
2、restful風(fēng)格接口書(shū)寫(xiě)方式
使用名詞表示資源使用單復(fù)數(shù)表示單個(gè)和多個(gè)資源使用正斜杠(/)表示層次關(guān)系使用連字符( - )來(lái)提高URI的可讀性在URI中使用小寫(xiě)字母在URI中使用小寫(xiě)字母
書(shū)寫(xiě)格式:標(biāo)準(zhǔn)的HTTP方法+URI
GET - /users返回用戶(hù)列表GET- /users/id返回特定用戶(hù)DELETE- /users/id刪除指定用戶(hù)POST- /users添加用戶(hù)PUT -/users/id更新指定用戶(hù)
3、restful風(fēng)格的實(shí)踐
本實(shí)踐基于springboot+mybatisplugs+maven
3.1、項(xiàng)目結(jié)構(gòu)
3.2、創(chuàng)建項(xiàng)目
使用官方推薦方式:Spring Initializr
我的Group為:com.duhong.learning
?回車(chē)下載壓縮包,再導(dǎo)入到idea中
3.3、配置
3.3.1、添加mybatis plugs依賴(lài)
在maven 庫(kù)(https://mvnrepository.com/)中查詢(xún)mybatis plugs依賴(lài)
pom.xml中添加依賴(lài)
3.3.2、 添加熱部署
1、pom.xml配置
2、配置設(shè)置
?3.3.3、配置application.yml?
說(shuō)明:要先刪除application.properties減少干擾,yml文件的優(yōu)先級(jí)大于properties文件,但都生效,后面properties文件會(huì)覆蓋yml文件。
#配置服務(wù)端口
server:
port: 80
#配置數(shù)據(jù)源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db3
username: root
password: 3237never
#配置mybatis plugs插件,設(shè)置數(shù)據(jù)庫(kù)id自增,默認(rèn)為ASSIGN_ID,使用id自增,簡(jiǎn)便配置
mybatis-plus:
global-config:
db-config:
id-type: auto
3.4、編碼
持久層
package com.duhong.learning.domain;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;
//lombok,一個(gè)java類(lèi)庫(kù),提供了一組注解,簡(jiǎn)化了pojo實(shí)體類(lèi)開(kāi)發(fā)
//@AllArgsConstructor
@Data//相當(dāng)于getter,setter,tostring
@TableName(value = "account")
public class User {
private Integer id;
private String name;
private Double balance;
}
數(shù)據(jù)操作層
package com.duhong.learning.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.duhong.learning.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
//@Mapper
//public interface UserDao {
// @Select("select * from account where id=#{id}")
// User getById(Integer id);
//}
@Mapper
public interface UserDao extends BaseMapper
}
服務(wù)接口層
package com.duhong.learning.service;
import com.duhong.learning.domain.User;
import java.util.List;
public interface UserService {
public User selectById(Integer id);
public List
public Integer deleteById(Integer id);
public Integer insert(User user);
public Integer updateById(User user,Integer id);
}
服務(wù)層
package com.duhong.learning.service.impl;
import com.duhong.learning.dao.UserDao;
import com.duhong.learning.domain.User;
import com.duhong.learning.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
private User user;
@Autowired
UserDao userDao;
@Override
public User selectById(Integer id) {
try{
user=null;
user=userDao.selectById(id);
}catch (Exception e){
System.out.println(e.getMessage());
}
return user;
}
@Override
public List
List
// for(User item :users){
// System.out.println(item.toString());
// }
return users;
}
//刪除,添加,更新指定用戶(hù),返回執(zhí)行條數(shù),可以用返回值查看是否操作成功
@Override
public Integer deleteById(Integer id) {
Integer sign=0;
try {
sign=userDao.deleteById(id);
}catch (Exception e){
System.out.println(e.getMessage());
}
return sign;
}
@Override
public Integer insert(User user) {
Integer sign=0;
try {
user.setName(user.getName());
user.setBalance(user.getBalance());
sign=userDao.insert(user);
}catch (Exception e){
System.out.println(e.getMessage());
}
return sign;
}
@Override
public Integer updateById(User user, Integer id) {
Integer sign=0;
try {
user.setId(id);
user.setName(user.getName());
user.setBalance(user.getBalance());
sign=userDao.updateById(user);
}catch (Exception e){
System.out.println(e.getMessage());
}
return sign;
}
}
控制層?
package com.duhong.learning.controller;
import com.duhong.learning.domain.User;
import com.duhong.learning.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//Restful風(fēng)格
@RestController
public class UserController {
@Autowired
UserServiceImpl userService;
@RequestMapping(value = "/users",method = RequestMethod.GET)
List
return userService.selectList();
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
User showById(@PathVariable Integer id){
return userService.selectById(id);
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
Integer deleteById(@PathVariable Integer id){
return userService.deleteById(id);
}
@PutMapping("/users/{id}")//換種方式,等效于@RequestMapping(value = "/users/{id}",method = RequestMethod.PUT)
Integer updataById(@RequestBody User user,@PathVariable Integer id){
return userService.updateById(user,id);
}
@PostMapping("/users")//接受User對(duì)象參數(shù)
Integer add(@RequestBody User user){
return userService.insert(user);
}
}
使用postman進(jìn)行測(cè)試
推薦vscode中使用Postcode插件,相當(dāng)方便
查詢(xún)所有
?添加數(shù)據(jù)
根據(jù)id查詢(xún)指定用戶(hù)
?
更新指定用戶(hù)
?
?刪除指定用戶(hù)
?感謝你閱讀到最后~?
?期待你的關(guān)注、收藏、評(píng)論、點(diǎn)贊~
??愿我們一起變強(qiáng)?
柚子快報(bào)邀請(qǐng)碼778899分享:后端 Restful風(fēng)格及實(shí)踐
精彩內(nèi)容
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。