Java Spring: How to handle HTTP Status 400?

1k views Asked by At

I'm trying to handle a HTTP Status Error 400. I think that this occurs due to a type mismatch. So e.g.
http://localhost:8080/app/info.htm?id=123 works while
http://localhost:8080/app/info.htm?id=abc doesn't.

@RequestMapping(value = "/info", method = RequestMethod.GET, params = "id")
public String getInfo(@RequestParam("id") Integer id, Model model) {
    // get info object via service and show it
    // model.addAttribute("infoObj", infoObj);

    return "info";
}

Is there a way to handle this and return e.g. the page index.jsp if this error occurs?

1

There are 1 answers

1
Jegg On BEST ANSWER

From what I understand that 400 error is that The request could not be understood by the server due to malformed syntax. That means id works good with int but not strings.

It does have the way that allows you to redirect the page to a certain page if error happned.

you need do some configs in web.xml file. you can do as follows:

<error-page>

    <error-code>400</error-code>
    <location>/index.html</location>
</error-page>