@RequestParam加与不加的区别

加上的情况:

    @RequestMapping("demo")
    public void test(@RequestParam String name){
        
    }

访问localhost:8080/demo,没有带上参数,会报错。可以通过设置@RequestParm里面的属性required属性为false,设置后不传参数也不会报错。

还可以指定属性defaultValue,如果设置了这个属性,没有设置required属性(即为true的情况下)。如果没有传参数,该参数会设置为defaultValue值

    @RequestMapping("demo")
    public void test(@RequestParam(defaultValue = "leo") String name){

    }

不加的时候:

    @RequestMapping("demo")
    public void test(String name){

    }

访问localhost:8080/demo,传不传参数name,都可以正常访问。

原文地址:https://www.cnblogs.com/tinyj/p/9786131.html