RESTFUL风格学习必看

RESTFUL风格编码

REST:Representational State Transfer,资源表现层状态转换,是⽬前⽐较主流的⼀种互联⽹软件架构,它结构清晰、标准规范、易于理解、便于扩展。
资源(Resource)
⽹络上的⼀个实体,或者说⽹络中存在的⼀个具体信息,⼀段⽂本、⼀张图⽚、⼀⾸歌曲、⼀段视频等等,总之就是⼀个具体的存在。可以⽤⼀个 URI(统⼀资源定位符)指向它,每个资源都有对应的⼀个特定的 URI,要获取该资源时,只需要访问对应的 URI 即可。

表现层(Representation)
资源具体呈现出来的形式,⽐如⽂本可以⽤ txt 格式表示,也可以⽤ HTML、XML、JSON等格式来表示。
状态转换(State Transfer)
客户端如果希望操作服务器中的某个资源,就需要通过某种⽅式让服务端发⽣状态转换,⽽这种转换是建⽴在表现层之上的,所有叫做"表现层状态转换"。

特点
URL 更加简洁。
有利于不同系统之间的资源共享,只需要遵守⼀定的规范,不需要进⾏其他配置即可实现资源共享。

在数据库curd操作:
GetMapping() 用于查询
PostMapping() 用于增加
PutMapping() 用于修改
DeleteMapping() 用于删除

Cntroller代码

package com.qust.controller;
import com.qust.dao.Impl.StudentDaoImpl;
import com.qust.dao.StudentDao;
import com.qust.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

//@RestController
@Controller
@ResponseBody
public class StudentController {
    private StudentDao studentDao = new StudentDaoImpl();
    @GetMapping("/student")
    public String studentList(){
        return "hello";
    }

    @GetMapping("/listStudent")
    public List<Student> student(){
        return studentDao.studentList();
    }

    @PostMapping("/add/{id}/{name}/{pwd}")
    public void Stu(@PathVariable int id,@PathVariable String name,@PathVariable String pwd){
        studentDao.studentAdd(new Student(id,name,pwd));
    }

    @DeleteMapping()
    public void xy(){
        
    }
}

@RestController 相当于 @Controller 和 @ResponseBody一起使用

需要解析成什么就添加相应的依赖
如 josn

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.10.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.10.0</version>
    </dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值