@RequestParam和@PathVariable在处理特殊字符时有什么区别?
+被@RequestParam接受为空格。
在@PathVariable的情况下,+被接受为+。
@RequestParam和@PathVariable在处理特殊字符时有什么区别?
+被@RequestParam接受为空格。
在@PathVariable的情况下,+被接受为+。
当前回答
1) @RequestParam用于提取查询参数
http://localhost:3000/api/group/test?id=4
@GetMapping("/group/test")
public ResponseEntity<?> test(@RequestParam Long id) {
System.out.println("This is test");
return ResponseEntity.ok().body(id);
}
而@PathVariable用于从URI中提取数据:
http://localhost:3000/api/group/test/4
@GetMapping("/group/test/{id}")
public ResponseEntity<?> test(@PathVariable Long id) {
System.out.println("This is test");
return ResponseEntity.ok().body(id);
}
2) @RequestParam在传统的web应用程序中更有用,其中数据主要通过查询参数传递,而@PathVariable更适合rest式web服务,其中URL包含值。
3)如果查询参数不存在或使用defaultValue属性为空,@RequestParam注释可以指定默认值,前提是所需的属性为false:
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/name")
String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {
return "Required element of request param";
}
}
其他回答
@RequestParam:我们可以说它是一个类似键值对的查询参数 @PathVariable:它来自URI
这两个注释的行为完全相同。
只有2个特殊字符!'和'@'被注释@ pathvariable和@ requestparam接受。
为了检查和确认行为,我创建了一个只包含1个控制器的spring引导应用程序。
@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;
}
}
点击以下请求,我得到了相同的响应:
localhost: 7000 / pvar /!@#$%^&*()_+-=[]{}|;':",./<>? localhost: 7000 / rpvar ?停止 =!@#$%^&*()_+-=[]{}|;':",./<>?
在两个请求中都收到了!@作为响应
1) @RequestParam用于提取查询参数
http://localhost:3000/api/group/test?id=4
@GetMapping("/group/test")
public ResponseEntity<?> test(@RequestParam Long id) {
System.out.println("This is test");
return ResponseEntity.ok().body(id);
}
而@PathVariable用于从URI中提取数据:
http://localhost:3000/api/group/test/4
@GetMapping("/group/test/{id}")
public ResponseEntity<?> test(@PathVariable Long id) {
System.out.println("This is test");
return ResponseEntity.ok().body(id);
}
2) @RequestParam在传统的web应用程序中更有用,其中数据主要通过查询参数传递,而@PathVariable更适合rest式web服务,其中URL包含值。
3)如果查询参数不存在或使用defaultValue属性为空,@RequestParam注释可以指定默认值,前提是所需的属性为false:
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping(value = "/name")
String getName(@RequestParam(value = "person", defaultValue = "John") String personName) {
return "Required element of request param";
}
}
@RequestParam用于查询参数(静态值),例如:http://localhost:8080/calculation/pow?base=2&ext=4
@PathVariable用于动态值,例如: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;
}
@PathVariable - must be placed in the endpoint uri and access the query parameter value from the request
@RequestParam - must be passed as method parameter (optional based on the required property)
http://localhost:8080/employee/call/7865467
@RequestMapping(value=“/call/{callId}", method = RequestMethod.GET)
public List<Calls> getAgentCallById(
@PathVariable(“callId") int callId,
@RequestParam(value = “status", required = false) String callStatus) {
}
http://localhost:8080/app/call/7865467?status=Cancelled
@RequestMapping(value=“/call/{callId}", method = RequestMethod.GET)
public List<Calls> getAgentCallById(
@PathVariable(“callId") int callId,
@RequestParam(value = “status", required = true) String callStatus) {
}