短信技术
1.新建一个Springboot项目
2.导入依赖
< dependency> < groupId> com.aliyun</ groupId> < artifactId> aliyun-java-sdk-core</ artifactId> < version> 4.5.3</ version>
</ dependency>
< dependency> < groupId> com.alibaba</ groupId> < artifactId> fastjson</ artifactId> < version> 1.2.75</ version>
</ dependency>
< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-data-redis</ artifactId>
</ dependency>
3.创建Service、ServiceImpl、Controller
package com. dyt. service ; import java. util. Map ; public interface SendSms { public boolean send ( String phoneNum, String TemplateCode , Map < String , Object > code) ;
}
package com. dyt. service. impl ; import com. alibaba. fastjson. JSON;
import com. aliyuncs. CommonRequest ;
import com. aliyuncs. CommonResponse ;
import com. aliyuncs. DefaultAcsClient ;
import com. aliyuncs. IAcsClient ;
import com. aliyuncs. exceptions. ClientException ;
import com. aliyuncs. exceptions. ServerException ;
import com. aliyuncs. http. MethodType ;
import com. aliyuncs. profile. DefaultProfile ;
import com. dyt. service. SendSms ;
import org. springframework. stereotype. Service ; import java. util. Map ; @Service
public class SendSmsImpl implements SendSms { @Override public boolean send ( String phoneNum, String TemplateCode , Map < String , Object > code) { DefaultProfile profile = DefaultProfile . getProfile ( "cn-hangzhou" , "LTAI4FsqKCo7EFEejszonBYn" , "密码" ) ; IAcsClient client = new DefaultAcsClient ( profile) ; CommonRequest request = new CommonRequest ( ) ; request. setSysMethod ( MethodType . POST) ; request. setSysDomain ( "dysmsapi.aliyuncs.com" ) ; request. setSysVersion ( "2017-05-25" ) ; request. setSysAction ( "SendSms" ) ; request. putQueryParameter ( "RegionId" , "cn-hangzhou" ) ; request. putQueryParameter ( "PhoneNumbers" , phoneNum) ; request. putQueryParameter ( "SignName" , "DYT" ) ; request. putQueryParameter ( "TemplateCode" , TemplateCode ) ; request. putQueryParameter ( "TemplateParam" , JSON. toJSONString ( code) ) ; try { CommonResponse response = client. getCommonResponse ( request) ; System . out. println ( response. getData ( ) ) ; return response. getHttpResponse ( ) . isSuccess ( ) ; } catch ( ServerException e) { e. printStackTrace ( ) ; } catch ( ClientException e) { e. printStackTrace ( ) ; } return false ; }
}
package com. dyt. controller ; import com. aliyuncs. utils. StringUtils ;
import com. dyt. service. SendSms ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. data. redis. core. RedisTemplate ;
import org. springframework. web. bind. annotation. CrossOrigin ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. PathVariable ;
import org. springframework. web. bind. annotation. RestController ; import java. util. HashMap ;
import java. util. UUID;
import java. util. concurrent. TimeUnit ; @RestController
@CrossOrigin
public class SendSmsApiController { @Autowired private SendSms sendSms; @Autowired private RedisTemplate < String , String > redisTemplate; @GetMapping ( "/send/{phone}" ) public String code ( @PathVariable ( "phone" ) String phone) { String code = redisTemplate. opsForValue ( ) . get ( phone) ; if ( ! StringUtils . isEmpty ( code) ) { return phone+ ":" + code+ "已存在,还没有过期" ; } code = UUID. randomUUID ( ) . toString ( ) . substring ( 0 , 4 ) ; HashMap < String , Object > map = new HashMap < > ( ) ; map. put ( "code" , code) ; boolean isSend = sendSms. send ( phone, "模版Code" , map) ; if ( isSend) { redisTemplate. opsForValue ( ) . set ( phone, code, 5 , TimeUnit . SECONDS) ; return phone+ ":" + code+ "发送成功" ; } else { return "发送失败" ; } }
}
4.配置application.properties
server.port= 9098 spring.redis.host= 127.0 .0.1
spring.redis.port= 6379
5.启动测试