@RequestParam vs @PathVariable

前端 未结 7 906
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:22

What is the difference between @RequestParam and @PathVariable while handling special characters?

+ was accepted by @Re

7条回答
  •  有刺的猬
    2020-11-22 06:37

    @RequestParam is use for query parameter(static values) like: http://localhost:8080/calculation/pow?base=2&ext=4

    @PathVariable is use for dynamic values like : http://localhost:8080/calculation/sqrt/8

    @RequestMapping(value="/pow", method=RequestMethod.GET)
    public int pow(@RequestParam(value="base") int base1, @RequestParam(value="ext") int ext1){
        int pow = (int) Math.pow(base1, ext1);
        return pow;
    }
    
    @RequestMapping("/sqrt/{num}")
    public double sqrt(@PathVariable(value="num") int num1){
        double sqrtnum=Math.sqrt(num1);
        return sqrtnum;
    }
    

提交回复
热议问题