cuốn sách gpt4 ai đã làm

java - 配置@PathVariable 类型不匹配的错误页面

In lại Tác giả: Walker 123 更新时间:2023-11-29 05:47:11 25 4
mua khóa gpt4 Nike

假设我有一个 Controller :

@Controller

public class SomeController {

@RequestMapping(value = "{id}/object.do")
public String showObject(@PathVariable("id") Integer id,
HttpServletRequest request) {
//do smth
return "somePage";
}
}

当“id”不是数字,而是像“aaa/object.do”这样的字符串时,Tomcat 向我显示错误 - “客户端发送的请求在语法上不正确。”

有没有办法配置一个错误页面,只有当“id”路径变量的类型不正确时才会显示?

câu trả lời hay nhất

Bạn có thể sử dụng @ExceptionHandler 来处理这个问题对于这个特定的错误(我怀疑是 TypeMismatchException )

@ExceptionHandler(TypeMismatchException.class)
public ModelAndView handleTypeMismatchException(TypeMismatchException ex) {
//TODO log
// Return something reasonable to the end user.
...
}

Xin lưu ý rằng@ExceptionHandler 与通常的处理程序基本上具有几乎相同的功能:

Much like standard controller methods annotated with a @RequestMapping annotation, the method arguments and return values of @ExceptionHandler methods are very flexible.

关于java - 配置@PathVariable 类型不匹配的错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15431180/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress