java如何通过两个操作对rest请求进行异常处理?
我正在创建Spring Boot应用程序,它有一个端点,可以根据一个请求执行两种类型的操作
第一步-发送电子邮件 第二步-发送短信
控制器类
@Override
public ResponseEntity<Object> sendAction(Action act) {
try {
actionService.createAlert(act);
return new ResponseEntity<>("Alert is generated successfully.", HttpStatus.CREATED);
}catch (Exception e) {
log.error("Exception occured while generating alerts. " , e);
return new ResponseEntity<>("Exception occured while generating alert.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
服务类别:
@Override
public void createAlert(Action act) throws Exception {
try {
String isSuccess = emailService.sendEmail(act);
}catch (Exception e) {
smsService.sendSMS(act,"Email Failed"); // Is this correct way?
}
smsService.sendSMS(act, "Email Success");
}
在这种情况下,我无法向客户区分电子邮件和短信是成功的还是仅仅是短信
我该怎么处理
提前谢谢
# 1 楼答案
让你的警报服务
然后,为
sendAction
请求定义一个响应,例如根据您的逻辑,用您想要的信息填充响应
在这种情况下,发送邮件和短信,但如果邮件失败,也尝试发送其他短信
在这种情况下,只有2^3=8种可能性存在,正在运行
你会得到