@RequestParam vs @PathVariable

前端 未结 7 1027
爱一瞬间的悲伤
爱一瞬间的悲伤 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:42

    Both the annotations behave exactly in same manner.

    Only 2 special characters '!' and '@' are accepted by the annotations @PathVariable and @RequestParam.

    To check and confirm the behavior I have created a spring boot application that contains only 1 controller.

     @RestController 
    public class Controller 
    {
        @GetMapping("/pvar/{pdata}")
        public @ResponseBody String testPathVariable(@PathVariable(name="pdata") String pathdata)
        {
            return pathdata;
        }
    
        @GetMapping("/rpvar")
        public @ResponseBody String testRequestParam(@RequestParam("param") String paramdata)
        {
            return paramdata;
        }
    }
    

    Hitting following Requests I got the same response:

    1. localhost:7000/pvar/!@#$%^&*()_+-=[]{}|;':",./<>?
    2. localhost:7000/rpvar?param=!@#$%^&*()_+-=[]{}|;':",./<>?

    !@ was received as response in both the requests

提交回复
热议问题