有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 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])