Java实现定时发送天气预报-创新互联
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
创新互联建站专注于企业成都全网营销、网站重做改版、吉林网站定制设计、自适应品牌网站建设、H5建站、成都商城网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为吉林等各大城市提供网站开发制作服务。Spring Boot
可以在Spring仓库中手动下载和安装。一种更为简便的方式是使用Groovy环境管理器(Groovy enVironment Manager,GVM),它会处理Boot版本的安装和管理。Boot及其CLI可以通过GVM的命令行gvm install springboot进行安装。在OS X上安装Boot可以使用Homebrew包管理器。为了完成安装,首先要使用brew tap pivotal/tap切换到Pivotal仓库中,然后执行brew install springboot命令。
快速创建实例
点击GENERATE
生产一个zip解压导入idea
即可
新建接收天气api的实体4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE com.github.ekko springboot-email 1.0.0 springboot-email Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-mail cn.hutool hutool-all 4.6.1 com.alibaba fastjson 1.2.70 org.projectlombok lombok 1.18.12 provided org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin aliyun aliyun https://maven.aliyun.com/repository/public true false spring-milestones Spring Milestones https://maven.aliyun.com/repository/spring true false spring-plugin spring-plugin https://maven.aliyun.com/repository/spring-plugin true false
package com.github.ekko.springtools.model;import lombok.Data;import lombok.NoArgsConstructor;import java.util.List;@Data@NoArgsConstructorpublic class Weather { private String day; private String date; private String week; //天气情况 private String wea; private String weaImg; private String air; private String humidity; // 空气质量 优 private String airLevel; // 空气质量描述:空气很好,可以外出活动,呼吸新鲜空气,拥抱大自然 private String airTips; private String tem1; private String tem2; private String tem; private Listhours;}
package com.github.ekko.springtools.model;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructorpublic class Whours { // 14日20时 private String day; //中雨 private String wea; //28℃ 实时温度 private String tem; //无持续风向 private String win; // 风速 3-4级 private String winSpeed;}天气接口
也没给我推广费,也作为我白嫖它这么久的回报吧
EmailService.java接口
package com.github.ekko.springtools.service;import com.github.ekko.springtools.model.Weather;import java.util.List;public interface EmailService { boolean sendSimpleMessage(); ListgetWeather();}
EmailService接口
package com.github.ekko.springtools.service.impl;import cn.hutool.http.HttpRequest;import cn.hutool.http.HttpUtil;import com.alibaba.fastjson.JSON;import com.github.ekko.springtools.model.Weather;import com.github.ekko.springtools.service.EmailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Service;import javax.mail.internet.MimeMessage;import java.util.ArrayList;import java.util.List;import java.util.Optional;@Servicepublic class EmailServiceImpl implements EmailService { private final static String FROM_MAIL = "你的发送邮箱,和配置文件中相同"; private final static String TO_MAIL = "接收人邮箱"; private final static String APPID = "你申请的天气api的appid,自行替换"; private final static String APPSECRET = "你申请的天气api的APPSECRET,自行替换"; public JavaMailSender emailSender; @Autowired public void setEmailSender(JavaMailSender emailSender) { this.emailSender = emailSender; } @Override public boolean sendSimpleMessage() { try { MimeMessage message = emailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true); mimeMessageHelper.setTo(TO_MAIL); mimeMessageHelper.setFrom(FROM_MAIL); mimeMessageHelper.setSubject("今日份天气到了~~"); mimeMessageHelper.setText(buildHtml(getWeather().get(0)), true); emailSender.send(message); } catch (Exception e) { e.printStackTrace(); return false; } return true; } public ListgetWeather() { HttpRequest httpRequest = HttpUtil.createGet("https://www.tianqiapi.com/api?version=v1&" + "appid=" + APPID + "&appsecret=" + APPSECRET + "&cityid=101020100"); String res = httpRequest.execute().body(); Object data = JSON.parseObject(res).get("data"); return JSON.parseArray(JSON.toJSONString(data), Weather.class); } private String buildHtml(Weather weather) { StringBuffer html = new StringBuffer(""); html.append("\n" + "\n" + "\n" + "\n" + " 文档标题 \n" + ""); if (weather.getWea().contains("雨")) { html.append("今日有雨,狗子请带伞!
"); } html.append("今日天气如下
" + ""); return html.toString(); }}
"); Optional.ofNullable(weather.getHours()) .orElse(new ArrayList<>()) .forEach(whours -> { html.append(" 时间 天气 温度 "); }); html.append(" ") .append(whours.getDay()) .append(" ") .append(whours.getWea()) .append(" ") .append(whours.getTem()) .append("
APPID与APPSECRET
查询其邮箱的SMTP
地址 ,链接 ,可以看到
使用SSL的通用配置如下: 接收邮件服务器:pop.qq.com,使用SSL,端口号995 发送邮件服务器:smtp.qq.com,使用SSL,端口号465或587 账户名:您的QQ邮箱账户名(如果您是VIP帐号或Foxmail帐号,账户名需要填写完整的邮件地址) 密码:您的QQ邮箱密码 电子邮件地址:您的QQ邮箱的完整邮件地址配置appliction.properties
server.port=9090 server.servlet.context-path=/mail spring.mail.host=smtp.qq.com spring.mail.port=465 spring.mail.username=你的邮箱地址 spring.mail.password=刚刚获取的授权码 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.starttls.enable=true控制层
@EnableScheduling定时任务
给指定方法设置时间表达式@Scheduled(cron = "0 0 8 * * ? ")
package com.github.ekko.springtools.controller;import com.github.ekko.springtools.model.Weather;import com.github.ekko.springtools.service.EmailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController@EnableSchedulingpublic class MailController { private EmailService emailService; @Autowired public void setEmailService(EmailService emailService) { this.emailService = emailService; } @GetMapping("/send") @Scheduled(cron = "0 0 23 * * ? ") public boolean sendEmail() { return emailService.sendSimpleMessage(); } @GetMapping("get-weather") public List启动类getWeather() { return emailService.getWeather(); }}
SpringbootEmailApplication即可
package com.github.ekko.springtools;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootEmailApplication { public static void main(String[] args) { SpringApplication.run(SpringbootEmailApplication.class, args); }}效果
本文标题:Java实现定时发送天气预报-创新互联
分享网址:http://ybzwz.com/article/cceddi.html