java - 如何通过 2 个动作对休息请求进行异常处理?
问题描述
我正在创建 Spring Boot 应用程序并有一个端点,它在单个请求上执行 2 种类型的操作。
第一个动作 - 发送电子邮件 第二个动作 - 发送短信
控制器类
@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");
}
在这种情况下,我无法向客户区分电子邮件和短信是否都成功或只是短信。
我该如何处理?
提前致谢。
解决方案
让您的警报服务
static class MailException extends Exception {}
static class SMSException extends Exception {}
static void mailService_send(String message) throws MailException {
if(message.length() > 5)
throw new MailException();
}
static void smsService_send(String message) throws SMSException {
if(message.length() > 5)
throw new SMSException();
}
然后,为您的sendAction
请求定义一个响应,例如
@ToString // lombok for simplicity
@AllArgsConstructor
static class SendActionResponse {
boolean mailSent;
boolean mailFailbackSMSSent;
boolean smsSent;
List<String> messages;
}
你的逻辑,用你想要的信息填充响应
static SendActionResponse sendAction(String mailMsg, String mailSmsFailBackMsg, String smsMsg) {
boolean mailSent = false;
boolean mailFailbackSMSSent = false;
boolean smsSent = false;
List<String> messages = new ArrayList<>();
try {
mailService_send(mailMsg);
messages.add("Mail sent");
mailSent = true;
} catch (MailException e1) {
try {
smsService_send(mailSmsFailBackMsg);
messages.add("Mail failure, failback sms sent");
mailFailbackSMSSent = true;
} catch (SMSException e2) {
messages.add("Mail failure, failback sms failure");
}
}
try {
smsService_send(smsMsg);
messages.add("Sms sent");
smsSent = true;
} catch (SMSException e3) {
messages.add("Sms failure");
}
return new SendActionResponse(mailSent, mailFailbackSMSSent, smsSent, messages);
}
在这种情况下,发送邮件和短信,但如果邮件失败,请尝试发送其他短信。
在这种情况下,只存在 2^3 = 8 种可能性,运行
String [] msg = new String [] {"short", "too long"};
for(String mail: msg)
for(String mailFailback: msg)
for(String sms: msg)
System.out.println(sendAction(mail, mailFailback, sms));
你会得到
SendActionResponse(mailSent=true, mailFailbackSMSSent=false, smsSent=true, messages=[Mail sent, Sms sent])
SendActionResponse(mailSent=true, mailFailbackSMSSent=false, smsSent=false, messages=[Mail sent, Sms failure])
SendActionResponse(mailSent=true, mailFailbackSMSSent=false, smsSent=true, messages=[Mail sent, Sms sent])
SendActionResponse(mailSent=true, mailFailbackSMSSent=false, smsSent=false, messages=[Mail sent, Sms failure])
SendActionResponse(mailSent=false, mailFailbackSMSSent=true, smsSent=true, messages=[Mail failure, failback sms sent, Sms sent])
SendActionResponse(mailSent=false, mailFailbackSMSSent=true, smsSent=false, messages=[Mail failure, failback sms sent, Sms failure])
SendActionResponse(mailSent=false, mailFailbackSMSSent=false, smsSent=true, messages=[Mail failure, failback sms failure, Sms sent])
SendActionResponse(mailSent=false, mailFailbackSMSSent=false, smsSent=false, messages=[Mail failure, failback sms failure, Sms failure])
推荐阅读
- java - 服务加载:如何在 Kotlin 原生上编写插件?
- swift - xcode12 ViewController.swift 窗口不完整显示
- architecture - 精细和粗糙位置之间的分离(定义)
- c# - 从工厂动态渲染 Blazor 组件
- spring-boot - Spring Boot with Jetty:禁用 oatomcat.util.scan.StandardJarScanner
- logging - 如何在使用 pm2 运行服务器时轮换日志文件?
- bluej - 在 Blue J 中更改员工编号(int 到 String 转换)时遇到问题?
- javascript - 电子商务网站购物车页面计算和HTMl更新
- gravitee - Gravitee - 从 API 管理门户更改文本
- javascript - 刷新页面时如何使喜欢按钮保持喜欢状态