当前位置:网站首页>REST风格
REST风格
2022-07-24 15:58:00 【白小筠】
REST风格
表现形式状态转换
优点
隐藏资源的访问行为,无法通过地址得知是对资源是何种操作
书写简化
http://localhost/users 查询全部用户信息 GET(查询)
http://localhost/users/1 查询指定用户信息 GET(查询)


http://localhost/users 添加用户信息 POST(新增/保存)
@RequestMapping(value = "/users",method = RequestMethod.POST)
@ResponseBody
public String save(){
System.out.println("user save...");
return "{'module': 'user save'}";
}

http://localhost/users 修改用户信息 PUT(修改/更新)
http://localhost/users/1 删除用户信息 DELETE(删除)
有参数,使用@PathVariable注解从路径中获取参数
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("user delete...id:"+id);
return "{'module': 'user delete'}";
}


@RequestBody @RequestParam @PathVariable区别:
@RequestParam 用于接受url地址传参或表单传参
@RequestBody 用于接受json数据
@PathVariable 用于接受路径参数,使用{参数名称}描述路径参数
简化操作:
//@Controller
//@ResponseBody
//两者合为下面一个
@RestController
@RequestMapping("/users")
public class UserController {
// @RequestMapping(method = RequestMethod.POST)
//替换为
@PostMapping
public String save() {
System.out.println("user save...");
return "{'module': 'user save'}";
}
// @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id) {
System.out.println("user delete...id:" + id);
return "{'module': 'user delete'}";
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@GetMapping("/{id}")
public String selectById(@PathVariable Integer id) {
System.out.println("user selectById...id:" + id);
return "{'module': 'user selectById'}";
}
}
案例:基于RESTful页面数据交互
1.先做后台功能,开发接口并调通接口
Controller类中写入两个功能(新建保存,查询所有)
@RestController
@RequestMapping("/books")
public class BookController {
@PostMapping
public String save(@RequestBody Book book){
System.out.println("book save!"+book);
return "{ module:'book save'}";
}
@GetMapping
public List<Book> getAll(){
List<Book> list=new ArrayList<>();
Book book1=new Book();
book1.setType("计算机");
book1.setName("SpringMVC入门教程");
book1.setDescription("小试牛刀");
Book book2=new Book();
book2.setType("计算机");
book2.setName("SpringMVC实战教程");
book2.setDescription("一代宗师");
Book book3=new Book();
book3.setType("计算机");
book3.setName("SpringMVC实战教程进阶");
book3.setDescription("一代宗师呕血创作");
list.add(book1);
list.add(book2);
list.add(book3);
return list;
}
}
在postman中调通接口
第一个功能

第二个功能


2.做页面异步调用,确认功能可以正常访问
//添加
saveBook () {
axios.post("/books",this.formData).then((res)=>{
});
},
//主页列表查询
getAll() {
axios.get("/books").then((res)=>{
this.dataList = res.data;
});
},
3.完成页面数据展示


点击确定返回到控制台查看
数据保存成功
4.补充:放行静态资源
创建一个类继承 WebMvcConfigurationSupport重写其中的addResourceHandlers方法
添加所需要放行的资源
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
//添加资源过滤
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
然后要在配置类中扫描该类
@Configuration
@ComponentScan({
"com.itheima.controller","com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
边栏推荐
- 20. Shell programming variables
- 未来数据库需要关心的硬核创新
- What is a firewall? What role can firewalls play?
- 【洛谷】P1908 逆序对
- 105 constructing binary trees from preorder and inorder traversal sequences
- Using JS to implement click events
- 【tf.keras】:版本从1.x升级到2.x遇到的一个问题:InvalidArgumentError: Cannot assign a device for operation embedding_
- Dynamics crm: sharing records for users and teams
- 简化理解:发布订阅
- [tf.keras]: a problem encountered in upgrading the version from 1.x to 2.x: invalidargumenterror: cannot assign a device for operation embedding_
猜你喜欢

Yolov6 trains its own data set

未来数据库需要关心的硬核创新

You can't just focus on flex layout and elaborate animation to explain all flex layout methods! Easy to understand dry goods tutorial

LaneATT

【AdaptiveAvgPool3d】pytorch教程

狗牙根植物介绍

Force button 31. Next arrangement -- double finger needling

MySQL之知识点(十二)
![[adaptiveavgpool3d] pytorch tutorial](/img/d0/60ee74ff554effa06084d5d01a03e1.png)
[adaptiveavgpool3d] pytorch tutorial

自适应设计和响应式设计
随机推荐
Memcache cache application (lnmp+memcache)
MySQL学习笔记(总结)
Do you understand the working principle of gyroscope?
Public and private key transmission, and understanding of CA certificate
Power of leetcode 231.2
[tf.keras]: a problem encountered in upgrading the version from 1.x to 2.x: invalidargumenterror: cannot assign a device for operation embedding_
【LOJ3247】「USACO 2020.1 Platinum」Non-Decreasing Subsequences(DP,分治)
Leetcode 223. rectangular area
Mlx90640 infrared thermal imager temperature measurement module development notes (III)
OpenMP入门
【SWT】滚动容器实现商品列表样式
Kubernetes static storage and dynamic storage
Feign for 20 minutes every day
从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
Dynamics 365: how to get the authentication information required to connect to D365 online from azure
Parse string
IP protocol - network segment division
Machine learning notes - building a recommendation system (5) feedforward neural network for collaborative filtering
Introduction to single chip microcomputer: LED bidirectional water lamp
JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue