【问题标题】:How to do exceptional handling for a rest request with 2 actions?如何通过 2 个动作对休息请求进行异常处理?
【发布时间】:2021-12-01 16:33:35
【问题描述】:

我正在创建 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");        
}

在这种情况下,我无法向客户区分电子邮件和短信是否都成功或只是短信。

我应该如何处理?

提前致谢。

【问题讨论】:

  • 什么时候发送短信?
  • 它应该在电子邮件之后发送,甚至在电子邮件失败之后发送。我想知道我怎样才能让客户区分这个
  • sendAction()的主要任务是什么?这意味着电子邮件和 SMS 的四种成功/失败组合中的哪一种意味着整体成功,哪些是失败?告知客户端最终用户故障模式(错误文本)是否足够,或者客户端软件是否需要对可能的故障模式(响应对象)做出不同的响应?
  • @RalfKleberhoff 是的,有 4 种可能的情况。我需要通过文本向客户端发送正确的模式。我可以通过从控制器中的电子邮件/短信服务获得成功响应来做到这一点。但是有没有更好的方法呢?

标签: java spring spring-boot exception httpresponse


【解决方案1】:

让您的警报服务

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])

【讨论】:

  • 谢谢@josejuan。让我试试这个
猜你喜欢
  • 2018-01-18
  • 2019-06-02
  • 2020-02-23
  • 2020-06-24
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多