1、获取JSON参数
java
@RequestMapping("/hello")
@RestController
public class HelloSpring {
@RequestMapping("/t10")
public String t10(@RequestBody UserInfo userInfo){
return userInfo.toString();
}
}
2、获取URI中的参数
(1)获取单个参数
java
@RequestMapping("/hello")
@RestController
public class HelloSpring {
@RequestMapping("/t11/{articleId}")
public String t11(@PathVariable Integer articleId){
return "articleId: " + articleId;
}
}
(2)获取多个参数
java
@RequestMapping("/hello")
@RestController
public class HelloSpring {
@RequestMapping("/t12/{name}/{age}")
public String t12(@PathVariable("name") String username,@PathVariable Integer age){
return "name: " + username + "; age: " + age;
}
}
3、获取文件
java
@RequestMapping("/hello")
@RestController
public class HelloSpring {
@RequestMapping("/f1")
public String f1(@RequestPart MultipartFile file){
return "获取文件的名字为:" + file.getOriginalFilename();
}
}
加上关键字@RequestPart,并将获取的文件添加到另一个目录下
java
@RequestMapping("/hello")
@RestController
public class HelloSpring {
@RequestMapping("/f2")
public String f2(@RequestPart MultipartFile file) throws IOException {
String filename = "/Users/liuwenwen/Desktop/学习/比特/Test" + file.getOriginalFilename();
file.transferTo(new File(filename));
return "成功获取文件的名字为:" + file.getOriginalFilename();
}
}