spring boot发送短信服务
2017-09-27 08:39
645 查看
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by jalen on 17-6-1. */ @Service public class SmsService { @Autowired RestTemplate restTemplate; @Value("${sms.username}") String username; @Value("${sms.password}") String password; @Value("${sms.url}") String url; public String sendMsg(String phoneNum,String text){ if (text.getBytes().length<=500){ String postUrl = url + "xxxx"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String,String> p = new LinkedMultiValueMap<>(); p.add("userId",username); p.add("password",password); p.add("pszMobis",phoneNum); p.add("pszMsg",text); HttpEntity< MultiValueMap<String,String>> entity = new HttpEntity< MultiValueMap<String,String>>(p,headers); String result = restTemplate.postForObject(postUrl,entity,String.class); return result; }else { String result = "短信内容过长,请重新编辑"; return result; } } }
2.看注入的属性RestTemplate对象实在哪里实例化的,这里实在spring boot入口Application处设置的bean
import com.xxx.repository.support.CustomRepository; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.web.client.RestTemplate; @SpringBootApplication //@EnableEncryptableProperties //启用配置文件加密Encrypt @EnableJpaAuditing //@EnableCaching //启用Ehcache缓存 @EnableJpaRepositories(repositoryBaseClass = CustomRepository.class) public class Application { @Bean RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3.@Value("${sms.username}")中值的获取,在配置文件yml中设置的
sms: username: xxxx password: xxxx url: http://xx.xx.xx.xx:8086/xx/xx.asmx/[/code]
相关文章推荐
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- Springboot实现阿里云通信短信服务有关短信验证码的发送
- SpringBoot+Security 发送短信验证码的实现
- SpringBoot --- 阿里大于发送短信工具类
- springboot 邮件接收发送 阿里云发送短信组件
- SpringBoot开发案例之整合mail发送服务