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:
A method can have any number of @PathVariable
annotations:
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:
@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) { // ... }
相关文章推荐
- URI Template Patterns with Regular Expressions
- spring uri template、ModelAndView、@ModelAttribute
- URI Template Patterns
- 【spring】RestTemplate发送请求
- spring 整合 redis,以及spring的RedisTemplate如何使用
- Spring jdcbtemplate实例
- springmvc No mapping found for HTTP request with URI 可能原因统计
- spring boot RestTemplate 后台发起http请求
- 配置spring+mybatis时出现Error creating bean with name 'sqlSessionTemplate‘
- Spring中使用TransactionTemplate的事务管理
- 是该抛弃Spring HibernateTemplate的时候了
- spring组件 RestTemplate + @ResponseBody 使用心得
- Spring(13):新增功能(SqlSessionTemplate方案):超市订单的供应商表查询 and 根据供应商名称查询供应商信息--功能实现(上)
- MyBatis-Spring整合 注入SqlSessionTemplate
- springMVC No mapping found for HTTP request with URI
- Spring : No mapping found for HTTP request with URI in DispatcherServlet with name "..."
- Spring MVC之@RequestMapping 详解【重点:指定的地址可以是URI Template 模式】
- Mongo整合SpringTemplate用法
- 使用RestTemplate Spring安全认证
- Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解