手机版

百科生活 投稿

java万年历,如何安装java(大牛教大家如何用SpringBoot技术快速实现天气预报系统)

百科 2026-02-14 12:48:57 投稿 阅读:9151次

关于【java万年历】:如何安装java,今天小编给您分享一下,如果对您有所帮助别忘了关注本站哦。

  • 内容导航:
  • 1、java万年历:如何安装java
  • 2、大牛教大家如何用SpringBoot技术快速实现天气预报系统

1、java万年历:如何安装java

  1、首先浏览器搜索java jdk,找到符合条件的网站,并进行下载,因为Oralce是国外的网站,发布的产品虽然新,但是下载比较慢,可以去国内的下载站点下载。

  2、下载完成后,会看到经典的Java安装工具。图标像一杯咖啡状。

  3、双击这个程序,进行安装,默认就一直“下一步”就可以了,默认会装在C盘的。非常快就安装好了这个开发包。

  4、鼠标右键打开我的电脑,找到“属性”。

  5、打开属性后,找到“高级”,找到下面的“环境变量”并打开。

  6、在环境变量中,要修改两个地方,一个是添加JAVA_HOME。可以选择“新建”,变量名填上JAVA_HOME,变量值填上C:Program FilesJavajdk1.7.0_04,因为在上面的安装过程中,我默认一直下一步安装,所以装在C盘,如果你在安装过程中改了,那可能是D盘或者E盘。同样变量值要做相应的更改。

  7、还要修改一个地方,就是Path,添加JAVA的变量值到Path中,选择Path,然后点“编辑”,在最后面添加如下语句;%JAVA_HOME%bin;

  8、环境变量已经设置好了,点击确定就可以了。现在开始测试一下,是不是真的可以了,打开DOS窗口,在左下角的“开始”中,“运行”输入“CMD”,打开DOS窗口,输入命令 java -version并回车就可以了。

2、大牛教大家如何用SpringBoot技术快速实现天气预报系统

从一个天气预报系统讲起

本节通过Spring Boot技术快速实现一个天气预报系统。

通过这个系统,一方面可以了解Spring Boot的全面用法,为后续创建微服务应用打下基础;另一方面,该系统会作为本节进行微服务架构改造的非常好的起点。

下面以前面创建的hello-world应用作为基础进行改造,成为新的应用micro-weather-basic。

java万年历,如何安装java(大牛教大家如何用SpringBoot技术快速实现天气预报系统)

开发环境

为了演示本例,需要采用如下开发环境。

. JDK8。

.Gradle 4.0。

. Spring Boot Web Starter 2.0.0.M4。

Apache HttpClient 4.5.3。

数据来源

天气的数据是天气预报的实现基础。本应用与实际的天气数据无关,理论上可以兼容多种数据来源。但为求简单,我们在网上找了一个免费、可用的天气数据接口。

  • ·天气数据来源为中华万年历。例如以下两种方式。

通过城市名称获得天气数据: http://wthrcdn.etouch.cn/weather_mini?city=深圳。

通过城市ID获得天气数据: http://wthrcdn.etouch.cn/weather_mini?citykey=101280601。

  • ·城市ID列表。每个城市都有一个唯一的ID作为标识,见https:/waylau.com/data/citylist.xml。

调用天气服务接口示例,这里以“深圳”城市为例,可看到如下天气数据返回。

{"data":{"yesterday":{"date" :"1日星期五","high" :"高温33℃","fx":"无持续风向","low" :"低温26℃","fl":"","type":"多云"},"city":"深圳","aqi" : "72","forecast":["date":"2日星期六","high":"高温32℃","fengli":"","low" :"低温26℃","fengxiang":"无持续风向","type" :"阵雨"},"date":"3日星期天","high":"高温 29℃","fengli":"","low" :"低温26℃","fengxiang":"无持续风向","type":"大雨""date":"4日星期一","high":"高温29℃","fengli":"","low":"低温26℃","fengxiang" :"西南风","type":"暴雨"},"date":"5日星期二","high":"高温31℃","fengli":"","low":"低温27℃","fengxiang":"无持续风向","type":"阵雨""date" :"6日星期三","high":"高温32℃","fengli":"","low":"低温27℃","fengxiang" :"无持续风向","type":"阵雨" }"ganmao":"风较大,阴冷潮湿,较易发生感冒,体质较弱的朋友请注意适当防护。" wendu":"29"},"status": 1000,"desc":"OK"}

通过观察以上数据,来理解每个返回字段的含义。

  • “city”:城市名称。
  • "aqi”:空气指数。
  • “wendu”:实时温度。
  • “date”:日期,包含未来5天。
  • “high”:最高温度。
  • “low”:最低温度。
  • “fengli”:风力。
  • “fengxiang”:风向。
  • “type”:天气类型。

以上数据是需要的天气数据的核心数据,但是,同时也要关注下面两个字段。

  • “status”:接口调用的返回状态,返回值“1000”,意味着数据接口正常。
  • ·“desc”:接口状态的描述,“OK”代表接口正常。

重点关注返回值不是“1000”的情况,这说明这个接口调用异常。

初始化一个Spring Boot项目

初始化一个Spring Boot项目“micro-weather-basic”,该项目可以直接以之前的“hello-world"应用作为基础进行修改。

添加Apache HttpClient的依赖,来作为Web请求的客户端。完整的依赖情况如下。

//依赖关系dependencies {//该依赖用于编译阶段compile('org.springframework.boot:spring-boot-starter-web')/添加Apache HttpClient依赖compile('org.apache.httpcomponents:httpclient:4.5.3')//该依赖用于测试阶段testCompile('org.springframework.boot:spring-boot-starter-test')}

java万年历,如何安装java(大牛教大家如何用SpringBoot技术快速实现天气预报系统)

创建天气信息相关的值对象

创建com.waylau.spring.cloud.weather.vo包,用于存放相关值对象。这些对象都是POJO对象,没有复杂的业务逻辑

创建天气信息类 Weather:

public class Weather implements Serializable {private static final long serialVersionUID - 1L;private string city;private String aqi;private String wendu;private string ganmao;private Yesterday yesterday;private Listforecast;1/省略getter/setter方法}

昨日天气信息类Yesterday :

public class Yesterday implements Serializable {private static final long serialversionUID = 1L;private string date;private string high;private String fx;private String low;private String fl;private String type;//省略getter/setter方法}

未来天气信息类Forecast:

public class Forecast implements Serializable private static final long serialVersionUID =1L;private string date;private string high;private string fengxiang;private string low;private String fengli;private String type;//省略getter/setter方法}

WeatherResponse作为整个消息的返回对象:

public class WeatherResponse implements Serializable{private static final long serialversionUID =1L;private Weather data;1/消息数据private String status;//消息状态private string desc;/l消息描述//省略getter/setter方法}

服务接口及实现

创建com.waylau.spring.cloud.weather.service包,用于存放服务接口及其实现。

下面是定义服务的两个接口方法,一个是根据城市的ID来查询天气数据,另一个是根据城市名称来查询天气数据。

package com.waylau.spring.cloud.weather.service;import com.waylau.spring.cloud.weather.vo.WeatherResponse;public interface weatherDataservice {WeatherResponse getDataByCityId(String cityId);WeatherResponse getDataByCityName(String cityName);}

其服务实现WeatherDataServiceImpl为:

package com.waylau.spring.cloud.weather.service;import java.io.IOException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;import com.fasterxml.jackson.databind.0bjectMapper;import com.waylau.spring.cloud.weather.vo.WeatherResponse;@servicepublic class WeatherDataServiceImpl implements WeatherDataService {Autowiredprivate RestTemplate restTemplate;private final string WEATHER_API = "http://wthrcdn.etouch.cn/weath-er_ mini";@overridepublic WeatherResponse getDataByCityId(string cityId){String uri = WEATHER_API + "?citykey=" + cityId;return this.doGetweatherData(uri);}@overridepublic WeatherResponse getDataByCityName(String cityName){String uri = WEATHER_API +"?city=" + cityName;return this.doGetWeatherData (uri);private WeatherResponse doGetWeatherData(String uri){ResponseEntity response = restTemplate.getForEntity(uri,String.class);String strBody = null;if(response.getstatusCodevalue()==200){strBody= response.getBody(;}objectMapper mapper = new objectMapper();WeatherResponse weather = null;try{weather = mapper.readValue (strBody,WeatherResponse.class);}catch (工OException e){e.printStackTrace();return weather;

其中:

. RestTemplate是一个REST客户端,默认采用Apache HttpClient来实现;

·返回的天气信息采用了Jackson来进行反序列化,使其成为WeatherResponse对象。

控制器层

创建com.waylau.spring.cloud.weather.service包,用于存放控制器层代码。控制器层暴露了RESTful API接口。

package com.waylau.spring.cloud.weather.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping:import org.springframework.web.bind.annotation.RestController;import com.waylau.spring.cloud.weather.service.WeatherDataService;import com.waylau.spring.cloud.weather.vo.WeatherResponse;/★大*天气AP工.** @since 1.0.0 2017年10月18日* author Way Lau*/@RestControllerRequestMapping("/weather")public class WeatherController {@Autowiredprivate WeatherDataService weatherDataService;@GetMapping("/cityId/{cityId}")public WeatherResponse getReportByCityId(CPathVariable("cityId")string cityId){return weatherDataService.getDataByCityId(cityId);}GetMapping("/cityName/{cityName}")public WeatherResponse getReportByCityName (CPathVariable ("cityName")string cityName){return weatherDataService.getDataByCityName(cityName);}

其中,@RestController会自动将返回的数据进行序列化,使其成为JSON数据格式。

配置类

创建com.waylau.spring.cloud.weather.config包,用于存放配置相关的代码。创建RestConfiguration

类,该类是RestTemplate 的配置类。

本文关键词:如何安装javafx,如何安装java环境eclipse,如何安装java运行环境,如何安装java虚拟机,如何安装java。这就是关于《java万年历,如何安装java(大牛教大家如何用SpringBoot技术快速实现天气预报系统)》的所有内容,希望对您能有所帮助!

本文链接:https://bk.89qw.com/a-562934

最近发表
网站分类