欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC框架REST架構(gòu)體系原理分析

 更新時間:2021年09月15日 14:27:46   作者:DrLai  
REST:Representational State Transfer,資源表現(xiàn)層狀態(tài)轉(zhuǎn)換,是目前⽐較主流的⼀種互聯(lián)網(wǎng)軟件架構(gòu),它結(jié)構(gòu)清晰、標(biāo)準(zhǔn)規(guī)范、易于理解、便于擴展

資源(Resource)

資源是網(wǎng)絡(luò)上的⼀個實體,或者說網(wǎng)絡(luò)中存在的⼀個具體信息,⼀段⽂本、⼀張圖片、⼀⾸歌曲、⼀段視頻等等,總之就是⼀個具體的存在。可以用⼀個 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)點:URL 更加簡潔。 有利于不同系統(tǒng)之間的資源共享,只需要遵守⼀定的規(guī)范,不需要進行其他配置即可實現(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)簡要分析的詳細內(nèi)容,更多關(guān)于SpringMVC框架REST架構(gòu)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論