首页 > spring @PathVariable问题

spring @PathVariable问题

能不能限制 getPet 方法 {petId} 只能的 a和b

@Controller
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
    // implementation omitted
}

可以使用正则进行约束,在变量名后面加上:即可, 如下:

@Controller
@RequestMapping(value = "/pets/{petId:a|b}", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public Pet getPet(@PathVariable String petId, Model model) {
    // implementation omitted
}

没懂你的意思

【热门文章】
【热门文章】