Pinyougou project notes: SpringBoot and Ali greater than

Posted by Imad on Mon, 29 Jun 2020 08:00:08 +0200

Notes on pinyougou project (11)



springboot is a back-end full stack framework (excluding page technology) produced by spring organization

Role: advocate zero configuration, do not need to integrate the framework structure, write business code directly, improve personnel utilization and development efficiency for enterprises.

Disadvantages: springboot is only suitable for small projects

Small example (1)

1, Create a normal project for maven
2, Introduce dependency and configure spring boot starter parent as parent project



3, Create the package structure and create a startup class in the upper package

4, Start class writing

package cn.itcast;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

 * Startup class
public class Application {
    public static void main(String[] args) {
        //Start the tomcat plug-in in the current project and run the current project,args);

5, Write a controller to test

public class TestController {

    public String hello(){
        return "helloWorld";

6, Run the main method of the startup class to start the project

Small examples (2)

1, Modify tomcat boot port
In the resources directory, create Document and add


2, Read properties file properties

Inject the property Environment and use getProperty("property name")

public class TestController {

    private Environment env;

    public String hello(){
        String url = env.getProperty("url");
        return "helloWorld"+url;

3, Hot deployment
No need to restart tomcat after modifying the code

Note: idea does not take effect, eclipse takes effect


Integration of springboot and activemq

1, Add coordinates


2, Configure avtivemq server
stay If not, the default message server is used

3, Write message sender

public class TestJms {
    private JmsMessagingTemplate jmsMessagingTemplate;

    public void send(String text){
        //Send message to message server
        //Parameter 1: specify queue name
        //Parameter 2: what to send
        jmsMessagingTemplate.convertAndSend("itcast", text);


4, Receiver who wrote the message

public class Consumer {

     * Define a listener to listen to messages sent from the message server
     * destination Is the queue from which the message is received
     * @param text
    public void readMessage(String text){
        System.out.println("Message received:"+text);


5, Sending and receiving messages

Ali greater than

1, Register alicloud account, application template and signature
2, Add a sub account and use the id and password of the sub account
3, SMS flow chart

4, Add SMS tool class

public class SmsUtil {
    //Product Name: cloud communication short message API product, developers do not need to replace it
    static final String product = "Dysmsapi";
    //Product domain name, developers do not need to replace
    static final String domain = "";

    private Environment env;

    // TODO here needs to be replaced by the developer's own AK (found on Alibaba cloud access console)

     * Send SMS
     * @param mobile cell-phone number
     * @param template_code Template No
     * @param sign_name autograph
     * @param param parameter
     * @return
     * @throws ClientException
    public SendSmsResponse sendSms(String mobile,String template_code,String sign_name,String param) throws ClientException {

        String accessKeyId =env.getProperty("accessKeyId");
        String accessKeySecret = env.getProperty("accessKeySecret");

        //Self adjusting timeout
        System.setProperty("", "10000");
        System.setProperty("", "10000");

        //Initialization of acsClient, region ization is not supported
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //Assembly request object - please refer to console - document for details
        SendSmsRequest request = new SendSmsRequest();
        //Required: mobile number to be sent
        //Required: SMS signature - can be found in SMS console
        //Required: SMS template - can be found in SMS console
        //Optional: the variables in the template replace the JSON string. For example, when the template content is "Dear ${name}, and your verification code is ${code}", the value here is

        //Optional - uplink SMS extension code (for users without special needs, please ignore this field)

        //Optional: outId is the extended field provided to the business party, and this value is finally brought back to the caller in the SMS receipt message

        //hint an exception may be thrown here. Pay attention to catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;


5, Use the message server to listen for messages

public class SmsListener {
    private SmsUtil smsUtil;

    public void sendSms(Map<String,String> map){
        try {
            SendSmsResponse response = smsUtil.sendSms(
                    map.get("param")  );
            System.out.println("Code=" + response.getCode());
            System.out.println("Message=" + response.getMessage());
            System.out.println("RequestId=" + response.getRequestId());
            System.out.println("BizId=" + response.getBizId());
        } catch (ClientException e) {


6, Send message

Send mobile phone number, template code, signature name and verification code respectively

    public void sendSms(){
        Map map=new HashMap();
        map.put("mobile", "cell-phone number");
        map.put("template_code", "Template id");
        map.put("sign_name", "Signature name");
        map.put("param", "{\"code\":\"123456\"}");


Topics: Spring Mobile SpringBoot Tomcat