[英]spring framework: correct way to handle multiple requestmappings with different pathvariables
我想將多個API端點路由到同一控制器方法。 但是,我在url端點中使用pathvariables。 例如:
@RequestMapping(value = {"/{x}/{y}/article/{z1}", "/{x}/{y}/page/{z2}"}, method = RequestMethod.GET)
@ResponseBody
public Response getZ(HttpServletRequest request, HttpServletResponse response,
@PathVariable("z1") String z1,
@PathVariable("z2") String z2,
@PathVariable("x") String x,
@PathVariable("y") String y,
@RequestParam(value = "A", required = true) String A) {
return new Response();
}
這是處理帶有路徑變量的多個URL的適當方法嗎? 有春季建議的方法可以完成此任務嗎?
如果存在z2,z1是否為null,反之亦然?
采用
@RequestMapping(value = {"/{x}/{y}/{cat:article|pages}/{zs:z1|z2}", method = RequestMethod.GET)
@ResponseBody
public Response getZ(HttpServletRequest request, HttpServletResponse response,@PathVariable("cat") String cat, @PathVariable("zs") String z){
// you code here
}
如上所示,您可以為同一個路徑變量設置許多值,這將使在代碼中輕松處理這些值變得容易。 希望這個回答你的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.