@GetMapping("/owners/{ownerId}/pets/{petId}")
public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
Pet pet = owner.getPet(petId);
model.addAttribute("pet", pet);
return "displayPet";
}
// GET /pets/42;q=11;r=22
@GetMapping("/pets/{petId}")
public void findPet(@PathVariable String petId, @MatrixVariable int q) {
// petId == 42
// q == 11
}
// GET /owners/42;q=11;r=12/pets/21;q=22;s=23
@GetMapping("/owners/{ownerId}/pets/{petId}")
public void findPet(
@MatrixVariable MultiValueMap<String, String> matrixVars,
@MatrixVariable(pathVar="petId"") MultiValueMap<String, String> petMatrixVars) {
// matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
// petMatrixVars: ["q" : 11, "s" : 23]
}
/*
public class User {
private String name;
private int age;
...
}
*/
#test 1
@PostMapping(value = "/test")
public User testAnnotation(@Validated @RequestBody User user){
return user;
}
#test 2
@PostMapping(value = "/test")
public String testAnnotation(@Validated @RequestBody String user){
return user;
}
//Get http://localhost:8989/test?name=lisi
@GetMapping(value = "/test")
public String testAnnotation(@RequestParam(value="name") String name){
return name;
}
@PostMapping(value = "/test")
public String testAnnotation(@RequestHeader("Accept-Encoding") String encoding) {
return encoding;
}
//JSESSIONID = 415A4AC178C59DACE0B2C9CA727CDD84
@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) {
//...
}
@PostMapping("/accounts")
public void handle(HttpEntity<Account> entity) {
// ...
}
启用MultipartResolver后,将解析具有multipart / form-data的POST请求的内容,并将其作为常规请求参数进行访问。
@Controller
public class FileUploadController {
@PostMapping("/form")
public String handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:uploadSuccess";
}
return "redirect:uploadFailure";
}
}
将参数类型声明为List
如果将@RequestParam批注声明为Map <String,MultipartFile>或MultiValueMap <String,MultipartFile>,但未在注释中指定参数名称,则将使用每个给定参数名称的多部分文件填充该映射。
@PostMapping(value = "/test/name/{name}/age/{age}")
public User testAnnotation(@ModelAttribute User user){
return user;
}
默认情况下,任何不是简单值类型(由BeanUtils#isSimpleProperty确定 )且未被其他任何参数解析器解析的参数都将被视为@ModelAttribute。
可以用来访问先前通过Servlet过滤器或HandlerInterceptor创建的设置的请求属性
可以用来访问先前通过Servlet过滤器或HandlerInterceptor创建的设置的请求属性
@Controller
public class DockerFileController {
@RequestMapping(value = "/test")
public User testAnnotation(@ModelAttribute User user){
User user1 =new User();
user1.setName("zs");
user1.setName("zs");
return user1;
}
}
报错,由于未使用@ResponseBody将会使用视图解析器解析返回对象,返回视图(如jsp页面等)不存在。换成@RestController即可
name # 映射器名称
value #映射请求路径,数组 可映射多个路径
method #请求类型,如 GET、POST、PUT、DELETE等;进阶注解如PostMapping、GetMapping等
consumes #
@PostMapping(path = "/pets", consumes = "application/json")
public void addPet(@RequestBody Pet pet) {
// ...
}
produces #指定返回的内容类型 ,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
@GetMapping(path = "/pets/{petId}", produces = {"application/xml;charset=UTF-8"})
@ResponseBody
public Pet getPet(@PathVariable String petId) {
// ...
}
params #指定request中必须包含某些参数值时该映射才匹配。
@GetMapping(path = "/pets/{petId}", params = "myParam=myValue")
public void findPet(@PathVariable String petId) {
// ...
}
header #指定request中必须包含某些指定的header值,才能让该方法处理请求。
@GetMapping(path = "/pets", headers = "myHeader=myValue")
public void findPet(@PathVariable String petId) {
// ...
}
原文:https://www.cnblogs.com/jinit/p/13867129.html