1
我有代码自定义异常:@ResponseStatus,客户端没有收到错误消息
@ResponseStatus(value = BAD_REQUEST, reason = "Login is busy")
public class LoginIsBusyException extends RuntimeException{
}
而一个方法,可以把它:
@RequestMapping(method = POST)
public void registration(@RequestBody UserRest user) throws
LoginIsBusyException{
userService.checkAlreadyExist(user.getLogin(), user.getMail());
user.setActive(false);
UserRest userRest = userService.addUser(user);
Integer randomToken = randomTokenService.getRandomToken(userRest.getMail());
mailService.sendMail(randomToken, userRest.getLogin(), userRest.getMail());
}
的问题是,客户端只接收错误代码但未收到状态文本“登录很忙”,已尝试添加捕获此异常的方法
@ExceptionHandler(LoginIsBusyException.class)
public void handleException(HttpServletResponse response) throws IOException
{
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Login is busy");
}
但是,该消息某处丢失,客户得到这样的回应:
非常感谢您的回答。但是我没有使用'@ ResponseBody',因为我使用了'@ RestController'注解。我以不同的方式解决了这个问更确切地说,我错了等待邮件在标题中。消息在json主体中。我在SoapUi得到回应后明白了这一点。自定义异常正常工作,这是我的错误。你的回答也是正确的。 –