当前位置:网站首页>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 {
}
边栏推荐
- [TA frost wolf \u may - hundred people plan] Figure 3.4 introduction to delayed rendering pipeline
- 31 next spread
- Using JS to implement click events
- Yolov3 trains its own data set
- 微调LayoutLM v3进行票据数据的处理和内容识别
- 简化理解:发布订阅
- Azure key vault (1) Introduction
- Will the capital market be optimistic about TCL's folding screen story?
- LaneATT
- Scala functions and their higher-order applications
猜你喜欢

Leetcode 220. duplicate element III exists

Mlx90640 infrared thermal imager temperature measurement module development notes (III)

From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?

Read the paper with me - multi model text recognition network

Dynamics 365: how to get the authentication information required to connect to D365 online from azure

With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!

Yolo5face: why reinvent the face detector

Fine tune layoutlm V3 for bill data processing and content recognition

Do you understand the working principle of gyroscope?

Using JS to implement click events
随机推荐
机器学习笔记 - 构建推荐系统(5) 前馈神经网络用于协同过滤
Parse string
Error 1053: the service did not respond to the start or control request in a timely fashion
Database learning – select (multi table joint query) [easy to understand]
With this machine learning drawing artifact, papers and blogs can get twice the result with half the effort!
[Luogu] p1908 reverse sequence pair
[shaders realize pixelate mosaic effect _shader effect Chapter 7]
yolov4 训练自己的数据集
R language Visual facet chart, multivariable grouping nested multilevel t-test, and specify reference level, visual multivariable grouping nested multilevel faceting boxplot, and add significance leve
JUC source code learning note 3 - AQS waiting queue and cyclicbarrier, BlockingQueue
【SWT】自定义数据表格
Hard core innovation that database needs to care about in the future
124 maximum path sum in binary tree
105 constructing binary trees from preorder and inorder traversal sequences
Withdrawal of IPO application, Yunzhou intelligent "tour" of unmanned boat enterprise fails to enter the science and Technology Innovation Board
AttributeError: module ‘seaborn‘ has no attribute ‘histplot‘
What is a firewall? What role can firewalls play?
faster-rcnn 训练自己的数据集
Choice of advanced anti DDoS IP and CDN in case of DDoS
【SWT】滚动容器实现商品列表样式