您的位置:首页 > 编程语言 > Java开发

spring URI Template Patterns

2017-02-09 09:18 302 查看
In Spring MVC you can use the
@PathVariable annotation on a method argument to bind it to the

value of a URI template variable:

@GetMapping("/owners/{ownerId}")
public String findOwner(@PathVariable String ownerId, Model model) {
Owner owner = ownerService.findOwner(ownerId);
model.addAttribute("owner", owner);
return "displayOwner";
}
To process the @PathVariable annotation, Spring MVC needs to find the matching URI template

variable by name. You can specify it in the annotation:
@GetMapping("/owners/{ownerId}")
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}

A method can have any number of @PathVariable
annotations:
@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";
}

The @RequestMapping
annotation supports the use of regular expressions in URI template variables.

The syntax is {varName:regex} where the first part defines the variable name and the second - the

regular expression. For example:
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(@PathVariable String version, @PathVariable String extension) {
// ...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: