如何使用spring boot成功注册用户后发送短信

时间:2017-05-23 05:40:11

标签: spring-boot sms using

创建帐户成功后,我必须向相关客户发送短信。

为此目的,我已经公开了短信服务作为建议如下。

package com.naresh.advice;

import javax.annotation.PostConstruct;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.naresh.dto.AccountDTO;
import com.naresh.dto.CustomerDTO;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;

@Component
@Aspect
public class SMSService {

    @Value("${twilio.sms.authentication_Id:80b7c5a8b73a26a9b588a906d54269c3}")
    private String authenticationId;
    @Value("${twilio.sms.account_sid:AC038d9532222b3d39fce4b43a5dce9ce1}")
    private String accountId;
    @Value("${twilio.sms.from_number:+12566662741}")
    private String fromNumber;

    @PostConstruct
    public void init() {
        Twilio.init(accountId, authenticationId);
    }

    @AfterReturning(pointcut = "execution(* com.naresh.service.impl.CustomerServiceImpl.save(..)) && args(customerDTO,..)", returning = "custId")
    public void sendSMS(JoinPoint joinPt, CustomerDTO customerDTO, Long custId) {

        Message.creator(new PhoneNumber(customerDTO.getMobile()), new PhoneNumber(fromNumber),
                "Customer " + custId + " registered successfully...").create();
    }

    @AfterReturning(pointcut = "execution(* com.naresh.service.impl.AccountServiceImpl.createAccount(..))", returning = "accDTO")
    public void sendSMSAcc(JoinPoint joinPt, AccountDTO accDTO) {

        CustomerDTO customerDTO = accDTO.getCustomer();

        Message.creator(new PhoneNumber(customerDTO.getMobile()), new PhoneNumber(fromNumber),
                "Hi " + customerDTO.getName() + ", Your " + accDTO.getAccountType() + " account " + accDTO.getAccNo()
                        + " has been registered with us successfully.Your balance is " + accDTO.getBalance())
                .create();
    }

}

如果帐户创建任务成功,上述工作正常。但是如果我们收到任何错误,那么客户也会收到成功的短信。

请帮帮我。

提前致谢

1 个答案:

答案 0 :(得分:0)

根据{{​​3}}:

@AfterReturning建议

  

仅在正常方法返回时调用,而不是在抛出异常时调用。

这意味着,您的方法com.naresh.service.impl.CustomerServiceImpl.savecom.naresh.service.impl.AccountServiceImpl.createAccount会返回一些值,但不会抛出任何异常。你得到的错误是什么?此错误是否会影响返回值?唯一的方法是解析返回的值以找出是否出错。