Having the following code:
@RequestMapping(value = "/system/login", method = RequestMethod.GET)
public void login(@RequestBody Login login) {
if(login.username == "test" && login.password == "test") {
//return HTTP 200
}
else {
//return HTTP 400
}
}
I would like to return two different HTTP statuses based on my logic. What is the best way to achieve this?
One approach which someone suggested at SO is to throw different exceptions which will be catch by different exception handlers:
See also:
BTW, your example code contains error:
login.password == "test"you should useequals()there 🙂Updated: I found another approach which even better because it doesn’t use exceptions:
See also ResponseEntity API