baris1892
baris1892

Reputation: 1051

Java Spring: How to handle HTTP Status 400?

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?

Upvotes: 0

Views: 1111

Answers (1)

Jegg
Jegg

Reputation: 549

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>

Upvotes: 1

Related Questions