@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) {
}
@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;
}
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);
}