<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.3.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.45</version> </dependency> </dependencies>
建立RestTemplate配置类,将RestTemplate注入容器中
/** * RestTemplate配置类 * @Author Sans * @CreateTime 2019/4/2 09:55 */ @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
Controller测试
/** * 测试短信DEMO * @Author Sans * @CreateTime 2019/4/2 09:39 */ @RestController @RequestMapping("/sms") public class TestController { @Autowired private RestTemplate restTemplate; /** * 单发短信测试 * @Author: Sans * @CreateTime: 2019/4/2 10:06 */ @RequestMapping(value = "/sendsmsTest",method = RequestMethod.GET) public String sendsmsTest(){ //单发短信API String url = "https://open.ucpaas.com/ol/sms/sendsms"; JSONObject jsonObject = new JSONObject(); //基础配置,在开发平台认证后获取 jsonObject.put("sid","ad024f8****************05d1614"); jsonObject.put("token","5ddbf62d4d****************e27402c"); jsonObject.put("appid","0ceaca4708****************76ec45f"); //模板ID,在开发平台创建模板对应的模板ID jsonObject.put("templateid", "432116"); //模板对应的参数,参数之间拼接用逗号作为间隔符 jsonObject.put("param", "1315,500"); //要发送的手机号 jsonObject.put("mobile", "用户的手机号"); //用户透传ID,随状态报告返回,可以不填写 jsonObject.put("uid",""); String json = JSONObject.toJSONString(jsonObject); //使用restTemplate进行访问远程服务 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<String> httpEntity = new HttpEntity<String>(json, headers); String result = restTemplate.postForObject(url, httpEntity, String.class); return result; } /** * 群发短信测试 * @Author: Sans * @CreateTime: 2019/4/2 11:23 */ @RequestMapping(value = "/sendBatchsmsTest",method = RequestMethod.GET) public String sendBatchsmsTest(){ //群发短信API String url = "https://open.ucpaas.com/ol/sms/sendsms_batch"; JSONObject jsonObject = new JSONObject(); //基础配置,在开发平台认证后获取 jsonObject.put("sid","ad024f8****************05d1614"); jsonObject.put("token","5ddbf62d4d****************e27402c"); jsonObject.put("appid","0ceaca4708****************76ec45f"); //模板ID,在开发平台创建模板对应的模板ID jsonObject.put("templateid", "432116"); //模板对应的参数,参数之间拼接用逗号作为间隔符 jsonObject.put("param", "1315,500"); //群发多个手机号之间要用逗号作为间隔符 jsonObject.put("mobile", "用户的手机号A,用户的手机号B"); //用户透传ID,随状态报告返回,可以不填写 jsonObject.put("uid",""); String json = JSONObject.toJSONString(jsonObject); //使用restTemplate进行访问远程服务 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); HttpEntity<String> httpEntity = new HttpEntity<String>(json, headers); String result = restTemplate.postForObject(url, httpEntity, String.class); return result; } }
https://www.jb51.net/article/160092.htm