Java制作一个天气应用(精解版)

917f64f889f64891b2cca1246f7f021a.webp

1. 准备工作

  • 环境要求
    • Java JDK
    • Maven
    • IDE(如IntelliJ IDEA或Eclipse)

2. 创建Spring Boot项目

  1. 使用Spring Initializr创建一个新的Spring Boot项目:

    • 访问 Spring Initializr
    • 选择项目元数据(如Group和Artifact)
    • 添加依赖:Spring Web 和 Spring Boot DevTools
    • 点击“Generate”下载项目。
  2. 解压下载的项目并在IDE中打开。

3. 添加依赖

pom.xml中添加对HTTP客户端的支持(如RestTemplate):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

4. 创建天气服务

创建一个服务类,用于调用天气API。

package com.example.weatherapp.service;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class WeatherService {
    private final String API_KEY = "YOUR_API_KEY"; // 替换为你的API密钥
    private final String API_URL = "https://api.openweathermap.org/data/2.5/weather";

    public String getWeather(String city) {
        RestTemplate restTemplate = new RestTemplate();
        String uri = UriComponentsBuilder.fromHttpUrl(API_URL)
                .queryParam("q", city)
                .queryParam("appid", API_KEY)
                .queryParam("units", "metric") // 使用摄氏度
                .toUriString();

        return restTemplate.getForObject(uri, String.class);
    }
}

5. 创建控制器

创建一个控制器以处理用户请求。

package com.example.weatherapp.controller;

import com.example.weatherapp.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/weather")
    public String getWeather(@RequestParam String city, Model model) {
        String weatherData = weatherService.getWeather(city);
        model.addAttribute("weatherData", weatherData);
        return "result";
    }
}

6. 创建视图

src/main/resources/templates目录下创建两个HTML文件。

  • index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weather App</title>
</head>
<body>
    <h1>Weather Application</h1>
    <form action="/weather" method="get">
        <input type="text" name="city" placeholder="Enter city name" required>
        <button type="submit">Get Weather</button>
    </form>
</body>
</html>
  • result.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weather Result</title>
</head>
<body>
    <h1>Weather Information</h1>
    <pre>${weatherData}</pre>
    <a href="/">Back</a>
</body>
</html>

7. 运行应用

  1. 在IDE中运行你的Spring Boot应用。
  2. 打开浏览器,访问 http://localhost:8080
  3. 输入城市名称并查看天气信息。

8. 改进功能

  • 解析JSON:使用Jackson库解析天气API返回的JSON数据,并以更友好的格式展示。
  • 错误处理:处理无效城市名称或API调用失败的情况。
  • 样式美化:使用CSS或前端框架(如Bootstrap)美化界面。

9. 获取API密钥

访问 OpenWeatherMap 注册并获取API密钥。

10. 部署

可以将应用部署到Heroku、AWS等云平台,方便他人访问。

63f0292dc8334bb4842f8772e56d494b.png

 

本文作者:GT工作室

如有疑问联系qq:3771822731

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值