其他分享
首页 > 其他分享> > 跟着黑马学SSM——Day7之REST风格

跟着黑马学SSM——Day7之REST风格

作者:互联网

REST风格

REST风格简介

注意事项

上述行为是约定形式,约定不是规范,可以打破,所以称REST风格,而不是REST规范

描述模块的名称通常使用复数,也就是加s的格式描述,表示此资源,而非单个资源,例如:users、books、accounts......

入门案例

步骤

  1. 设定http请求动作(动词)

    @Controller
    @RequestMapping("/user")
    public class UserController {
        @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
        @ResponseBody
        public String save(@PathVariable int id) {
            System.out.println("user save..." +id);
            return "{'module':'save'}";
        }
    
        @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
        @ResponseBody
        public String delete() {
            System.out.println("user delete...");
            return "{'module':'user delete'}";
        }
        @RequestMapping(value = "/users",method = RequestMethod.POST)
        @ResponseBody
        public String list(@RequestBody User user) {
            System.out.println("user delete..."+ user);
            return "{'module':'"+user+"'}";
        }
    }
    
  2. 设定请求参数(路径变量)

    @RequestMapping(value = "/users/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String save(@PathVariable int id) {
        System.out.println("user save..." +id);
        return "{'module':'save'}";
    }
    

@RequestBody @RequestParam @PathVariable

  • 区别
    • @RequestBody用于接收url地址传参或表单传参
    • @RequestParam用于接收json数据
    • @PathVariable用于接收路径参数,使用{参数名称}描述路径参数
  • 应用
    • 后期开发中,发送请求参数超过一个时,以json格式为主,@RequestBody应用较广
    • 如果发送非json格式数据,选用@RequestParam接收请求参数
    • 采用RESTful进行开发,当参数数量较少时,例如一个,可以采用@PathVariable接收请求路径变量,通常用于传递id值

案例:基于RESTful页面数据交互

  1. 制作SpringMVC控制器,并通过PostMan测试接口功能

    @RestController
    @RequestMapping("/books")
    public class BookController {
        @PostMapping
        public String save(@RequestBody Book book){
            System.out.println("book save==>" + book);
            return "{'module':'book save success'}";
        }
        @GetMapping
        public List<Book> getAll(){
            System.out.println("book getAll is running ...");
            List<Book> bookList=new ArrayList<>();
            Book book = new Book();
            book.setType("计算机");
            book.setName("SpringMVC入门教程");
            book.setDescription("小试牛刀");
            Book book1 = new Book();
            book1.setType("计算机");
            book1.setName("SpringMVC实战教程");
            book1.setDescription("一代宗师");
            bookList.add(book);
            bookList.add(book1);
            return bookList;
        }
    }
    
  2. 设置对静态资源的访问放行

    @Configuration
    public class SpringMvcSupport extends WebMvcConfigurationSupport {
        //设置静态资源访问过滤,当前类需要设置为配置类,并被扫描加载
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            //当访问/pages/????时候,从/pages目录下查找内容
            registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
            registry.addResourceHandler("/js/**").addResourceLocations("/js/");
            registry.addResourceHandler("/css/**").addResourceLocations("/css/");
            registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
        }
    }
    
  3. 前端页面通过异步提交访问后台控制器

    // 弹出添加窗口
    openSave() {
        this.dialogFormVisible = true;
        this.resetForm();
    },
    
    //添加
    saveBook () {
        axios.post("/books",this.formData).then((res)=>{
    
        });
    },
    
    //主页列表查询
    getAll() {
        axios.get("/books").then((res)=>{
            this.dataList = res.data;
        });
    

标签:----,http,Day7,REST,SSM,book,user,public,localhost
来源: https://www.cnblogs.com/ltom/p/16672503.html