Java 如何在Spring MVC异常处理程序中访问@PathVariable?
这样做有效吗:Java 如何在Spring MVC异常处理程序中访问@PathVariable?,java,spring,spring-mvc,Java,Spring,Spring Mvc,这样做有效吗: @RequestMapping(method = RequestMethod.POST) public @ResponseBody Post doSomething(@PathVariable postId) { } @ExceptionHandler(Exception.class) private ModelAndView handleException(Exception ex, @PathVariable postId) { } 当我向Spring的方法签名中添加
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Post doSomething(@PathVariable postId) {
}
@ExceptionHandler(Exception.class)
private ModelAndView handleException(Exception ex, @PathVariable postId) {
}
当我向Spring的方法签名中添加@PathVariable时,Spring似乎忽略了我的异常处理程序。我认为这种信息应该包含在异常本身中
我不知道异常处理程序获取路径变量。@PathVariable在@ExceptionHandler方法中确实不受支持。但是,在Spring3.1中,您应该能够使用PathVariableMethodArgumentResolver配置ExceptionHandlerExceptionResolver,这将使它能够工作。请随意在中打开一张票,这似乎是我们可以添加的内容。也许是这样?控制器的一种设计方法
private Optional<String> getPathVariable(String paramName) {
return Optional.ofNullable((Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE))
.filter(map -> map.containsKey(paramName))
.map(map -> map.get(paramName));
}