14
有没有一种方法可以自定义当所需的@RequestParam
未发送到请求处理程序时显示的内容?我始终得到HTTP状态400,其描述为“”在这种情况下,客户端发送的请求在语法上不正确()。“。如何在Spring MVC中自定义@RequestParam错误400响应
有没有一种方法可以自定义当所需的@RequestParam
未发送到请求处理程序时显示的内容?我始终得到HTTP状态400,其描述为“”在这种情况下,客户端发送的请求在语法上不正确()。“。如何在Spring MVC中自定义@RequestParam错误400响应
是的,有一种方法,你应该抓住MissingServletRequestParameterException
你能做到在几个方面:
1)
@ExceptionHandler(MissingServletRequestParameterException.class)
public String handleMyException(Exception exception) {
return "yourErrorViewName";
}
2)
<error-page>
<exception-type>org.springframework.web.bind.MissingServletRequestParameterException</exception-type>
<location>/WEB-INF/pages/myError.jsp</location>
</error-page>
希望它帮助。
我如何解决我的问题:
@ResponseBody
@ExceptionHandler(MissingServletRequestParameterException.class)
public Object missingParamterHandler(Exception exception) {
// exception handle while specified arguments are not available requested service only. it handle when request is as api json service
return new HashMap() {{ put("result", "failed"); put("type", "required_parameter_missing");}};
}