SpringBoot——两种传参方式

  1. ?传参

    举例:http://localhost:8082/news/asset/getDatas?page=1&keyWord=123&year=2020

    注解:@RequestParam

    使用:

    @RequestMapping("/getDatas")
    public String getDatas(@RequestParam("page") int page, @RequestParam("keyWord") String keyWord, @RequestParam("year") String year){
        return assetService.getDatas(page,keyWord,year);
    }
    

    注:@RequestParam可以选择request=false来允许初值为空,但这个值的类型必须为对象,不能为基本类型

  2. Restful风格传参

    举例:http://localhost:8082/news/asset/getDatas/1/123/2020

    注解:@PathVariable

    使用:

    @RequestMapping("/getDatas/{page}/{keyWord}/{year}")
    public String getDatas(@PathVariable("page") int page,@PathVariable("keyWord") String keyWord,@PathVariable("year") String year){
        return assetService.getDatas(page,keyWord,year);
    }
    

    注:@PathVariable绑定的是@RequestMapping中的变量

原文地址:https://www.cnblogs.com/Arno-vc/p/13442430.html