get请求
1.请求的 URL 为http://localhost:8080/get?name=doge&age=26,使用@RequestParam
@GetMapping(path = "/get1")
public void get1(@RequestParam(name = "name") String name,
@RequestParam(name = "age") Integer age) {
log.info("name:{},age:{}", name, age);
}
2. 请求的 URL 为http://localhost:8080/get/doge/26,使用@PathVariable
@GetMapping(path = "/get2/{name}/{age}")
public void get1(@PathVariable(value = "name") String name,
@PathVariable(value = "age") Integer age) {
log.info("name:{},age:{}", name, age);
}
Post请求
1.传递数据为json格式时,使用@RequestBody
@GetMapping(path = "/post")
public void get1(@RequestBody User user) {
log.info(user.toString);
}
2.传递数据为文件类型时,使用MultipartFile
@PostMapping(value = "/file1")
public String file1(MultipartFile multipartFile) {
log.info(multipartFile.getName());
return content;
}