C'è un modo per personalizzare ciò che viene visualizzato quando un richiesto @RequestParam
non viene inviato al gestore richieste? Ottengo sempre HTTP Status 400 con una descrizione "La richiesta inviata dal client era sintatticamente errata()." in questo caso.Come personalizzare @RequestParam errore 400 risposta in Spring MVC
14
A
risposta
16
Sì, c'è un modo si dovrebbe prendere MissingServletRequestParameterException
È possibile farlo in diversi modi:
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>
spero che aiuta.
4
Come ho risolto il mio problema:
@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");}};
}