How to implement SMS sending service in Spring-Boot.
4 min readMay 10, 2021
using Twilio web services.
There are few ways to implement SMS sending service in Spring-Boot. Today we are going to discuss how to implement it by using Twilio online web service.
- Through this blog I use the MVC architecture to implementations.
- EntelliJ IDEA ultimate 2020.3.2 use for Spring-boot implementations.
- Required web site links and references added at the end of the blog.
1. As the first step you need to create a account in Twilio web service.
- By clicking sign-up button you can create an free account.
- After creating the account you can see the configuration details and mobile number which get you from the twilio. That is the number we have to use in our SMS service.
2. Add dependencies to the Spring-Boot project
- You have to add below dependencies inside the
pom.xml
file in the project.
<dependency>
<groupId>com.twilio.sdk</groupId>
<artifactId>twilio</artifactId>
<version>7.16.1</version>
</dependency>
3. Implement the SMS service.
- Create an
SMSFeedback
class inside the model class. That is the model of SMS feedback object which is sending from the frontend of the Backend application. - Generate the setters and getters from the declared attributes also.
- Create a Feedback controller class inside the controllers and declare two variables to assign Twilio account id and token.
- Before the next implementations you have to import below dictionaries to the class. Same as need to import
SMSFeedack
class here.
If you use the IntelliJ Idea for this implementations, no need to add those imports to class because it automatically add them to it. otherwise you have to add them manually.
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
- After that you have to create a message according to the API request and send it.
Message.creator
function order is given below.
Message.creator(new PhoneNumber("toNumber"),
new PhoneNumber("fromNumber"), "Message").create();
- Now SMS service implementation part is almost done. Now you need to test the service working properly.
4. Testing the Service work properly
- To do this testing part, you can use third-party software like postman or create a testing file inside the project.
- In this time I use the testing file to test the service work properly.
- To do that, create a file call
SMSFeedback.http
inside the project and create a API request to the SMS service part like below snapshot.
- Then run the Spring-Boot project. And run this testing file next.
- If it is working properly you will get the response like given below.
- Then check your mobile phone, you will get a message.
Required Website links:
- Twilio signup : https://www.twilio.com
- If you want to know what is the MVC architecture is, please check this one : https://minindukothalawala.medium.com/mvc-architecture-7145ee90ef0f
Thank you for reading this blog and I hope you get an idea about the SMS service implementation in Spring-Boot. Please feel free to leave a comment if you have any doubts according to this implementation, I will try to help you as I can. Stay safe.