首页 > 解决方案 > 如何通过 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");        
}

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

我该如何处理?

提前致谢。

标签: javaspringspring-bootexceptionhttpresponse

解决方案


让您的警报服务

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

推荐阅读