SpringMVC框架REST架構(gòu)體系原理分析
資源(Resource)
資源是網(wǎng)絡(luò)上的⼀個實(shí)體,或者說網(wǎng)絡(luò)中存在的⼀個具體信息,⼀段⽂本、⼀張圖片、⼀⾸歌曲、⼀段視頻等等,總之就是⼀個具體的存在??梢杂?#12032;個 URI(統(tǒng)⼀資源定位符)指向它,每個資源都有對應(yīng)的⼀個 特定的 URI,要獲取該資源時,只需要訪問對應(yīng)的 URI 即可。
表現(xiàn)層(Representation)
資源具體呈現(xiàn)出來的形式,⽐如⽂本可以⽤ txt 格式表示,也可以⽤ HTML、XML、JSON等格式來表 示。
狀態(tài)轉(zhuǎn)換(State Transfer)
客戶端如果希望操作服務(wù)器中的某個資源,就需要通過某種⽅式讓服務(wù)端發(fā)⽣狀態(tài)轉(zhuǎn)換,而這種轉(zhuǎn)換是 建⽴在表現(xiàn)層之上的,所有叫做"表現(xiàn)層狀態(tài)轉(zhuǎn)換"
Rest的優(yōu)點(diǎn):URL 更加簡潔。 有利于不同系統(tǒng)之間的資源共享,只需要遵守⼀定的規(guī)范,不需要進(jìn)行其他配置即可實(shí)現(xiàn)資源共享
如何使用
如何使⽤ REST 具體操作就是 HTTP 協(xié)議中四個表示操作⽅式的動詞分別對應(yīng) CRUD 基本操作。 GET ⽤來表示獲取資源。 POST ⽤來表示新建資源。 PUT ⽤來表示修改資源。 DELETE ⽤來表示刪除資源。
1.在Handler寫出增刪改查的方法
package Mycontroller;
import entity.Student;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import repository.StudentRepository;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
@RequestMapping("/rest")
@RestController
public class RestHandler {
@Autowired
private StudentRepository studentRepository;
@RequestMapping(value = "/findAll",method = RequestMethod.GET)
// @GetMapping("/findAll")
public Collection<Student> findAll(HttpServletResponse response){
response.setContentType("text/json;charset=UTF-8");
return studentRepository.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") long id)
{
return studentRepository.findById(id);
}
@PostMapping("/sava")
public void save(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentRepository.saveOrUpdate(student);
}
@DeleteMapping("/deleteById/{id}")
public void deleteById(@PathVariable("id") long id){
studentRepository.deleteById(id);
}
}
2.Repository
@Repository
public class StudentRepositoryImpl implements StudentRepository {
private static Map<Long, Student> studentMap;
static {
studentMap=new HashMap<>();
studentMap.put(1L,new Student (1L,"zhangsan",22));
}
@Override
public Collection<Student> findAll() {
return studentMap.values();
}
@Override
public Student findById(long id) {
return studentMap.get(id);
}
@Override
public void saveOrUpdate(Student student) {
studentMap.put(student.getId(),student);
}
@Override
public void deleteById(long id) {
studentMap.remove(id);
}
}
以上就是SpringMVC框架REST架構(gòu)簡要分析的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC框架REST架構(gòu)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java局部變量表的基礎(chǔ)知識點(diǎn)及實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于java局部變量表的基礎(chǔ)知識點(diǎn)及實(shí)例,有需要的朋友們可以學(xué)習(xí)參考下。2021-06-06
SpringBoot 在IDEA中實(shí)現(xiàn)熱部署步驟詳解(實(shí)用版)
這篇文章主要介紹了SpringBoot 在IDEA中實(shí)現(xiàn)熱部署步驟詳解(實(shí)用版),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面
這篇文章主要介紹了play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面的相關(guān)資料,需要的朋友可以參考下2016-11-11
Java中StringBuilder字符串類型的操作方法及API整理
Java中的StringBuffer類繼承于AbstractStringBuilder,用來創(chuàng)建非線程安全的字符串類型對象,下面即是對Java中StringBuilder字符串類型的操作方法及API整理2016-05-05
Java通過調(diào)用C/C++實(shí)現(xiàn)的DLL動態(tài)庫——JNI的方法
這篇文章主要介紹了Java通過調(diào)用C/C++實(shí)現(xiàn)的DLL動態(tài)庫——JNI的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01
詳解spring boot jpa整合QueryDSL來簡化復(fù)雜操作
這篇文章主要介紹了詳解spring boot jpa整合QueryDSL來簡化復(fù)雜操作,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

