提示:以下是本篇文章正文内容,下面案例可供参考一、准备事项(以下为目录结构)
注意:config可以不用新建,里面配置类可有可无
二、使用步骤 1.引入库
pom.xml依赖:
org.springframework.boot spring-boot-starter-data-mongodborg.springframework.boot spring-boot-starter-testtest org.projectlombok lombokorg.springframework.boot spring-boot-devtoolstrue com.alibaba druid1.1.16
Controller代码如下(示例):
package com.example.controller;
import com.example.Service.SMSCService;
import com.example.dao.SMSCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/sms")
public class SMSCodeController {
@Autowired
private SMSCService smscService;
@ApiOperation("发验证码")
@GetMapping
public String getCode(String tele){
String code = smscService.sendCodeToSMS(tele);
System.out.println(code);
return code;
}
@PostMapping("/checkCode")
public boolean checkCode(SMSCode smsCode){
return smscService.checkCode(smsCode);
}
@ApiOperation("HELLO")
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
dao层代码
package com.example.dao;
import lombok.Data;
@Data
public class SMSCode {
private String tele;
private String code;
}
Service接口代码:
package com.example.Service;
import com.example.dao.SMSCode;
public interface SMSCService {
String sendCodeToSMS(String tele);
boolean checkCode(SMSCode smsCode);
}
ServiceImpl接口实现类代码:
package com.example.Service.impl;
import com.example.Service.SMSCService;
import com.example.dao.SMSCode;
import com.example.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
@Service
public class SMSCServiceImpl implements SMSCService {
@Autowired
private CodeUtils codeUtils;
@Override
@CachePut(value = "smscode",key = "#tele")
public String sendCodeToSMS(String tele) {
String c = codeUtils.GenerCode(tele);
return c;
}
@Override
public boolean checkCode(SMSCode smsCode) {
return false;
}
}
CodeUtils代码:
package com.example.utils;
import org.springframework.stereotype.Component;
@Component
public class CodeUtils {
private String[] num={"00000","0000","000","00","0",""};
public String GenerCode(String number){
int hash = number.hashCode();
int encry=12464374;
long result = hash ^ encry;
long nowTime=System.currentTimeMillis();
result=result^nowTime;
long code = result % 10000;
code= code<0 ?-code:code;
String resultStr=code+"";
int length = resultStr.length();
return num[length]+resultStr;
}
}
Application代码:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
@SpringBootApplication
@EnableCaching
public class PhonenumberApplication {
public static void main(String[] args) {
SpringApplication.run(PhonenumberApplication.class, args);
}
}
2.测试
3.结果浏览器输入:localhost:8080/sms?tele=123456789
注意:123456789为测试数据
总结
当作一个小玩意玩玩,原链接为B站视频第109集 :
黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)_哔哩哔哩_bilibili
如果想要源码的小伙伴吗,可以私信我,免费白嫖的哦;



