Announcement Announcement Module
No announcement yet.
How to handle an invalid @PathVariable Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to handle an invalid @PathVariable

    I have a controller:

    @RequestMapping(value="/pet/{id}", method=RequestMethod.GET)
    public String show(@PathVariable("id") Long id, ModelMap modelMap) {
    //Use id to get details
    return "pet/details";

    I would like to be able to handle an invalid @PathVariable in my controller method instead of a 400 page being displayed.

    An invalid @RequestParam can be handled by including a BindingResult in the method signature. Is it possible to do anything like this with a @PathVariable? When I add a BindingResult argument to my method I get:

    "java.lang.IllegalStateException: Errors/BindingResult argument declared without preceding model attribute. Check your handler method signature!"

    It would seem to me adventitious to manage make path variables in the exact same way @RequestParam's are handled. Both in the way the controller can handle invalid values and in how request parameters can be bound to model objects using @ModelAttribute.