How to customize @RequestParam error 400 response in Spring MVC

前端 未结 2 573
心在旅途
心在旅途 2020-12-16 14:12

Is there a way to customize what gets displayed when a required @RequestParam is not sent to the request handler? I always get HTTP Status 400 with a descriptio

2条回答
  •  暖寄归人
    2020-12-16 15:02

    Yes, there is a way you should catch MissingServletRequestParameterException

    You can do it in several ways:

    1)

       @ExceptionHandler(MissingServletRequestParameterException.class)
          public String handleMyException(Exception  exception) {
           return "yourErrorViewName";
                  }  
    

    2)

    
        org.springframework.web.bind.MissingServletRequestParameterException
        /WEB-INF/pages/myError.jsp
      
    

    Hope it helps.

提交回复
热议问题