我使用 OpenWeather API 制作了一個應用程式,您可以在其中搜索要查看其天氣資訊的城市(溫度、天氣描述、最低溫度...)。我做了這部分,但是當我嘗試添加可以看到 5 天 / 3 小時預報天氣的預報選項時,我卡住了。
這是我收到的回復
正確的回應將顯示一個表格,每 3 小時顯示一次天氣資訊,但現在我只收到一行包含一個日期資訊。
我嘗試了一些回圈,但沒有奏效。
ForecastWeatherController.java
@Controller
@RequestMapping("/")
public class ForecastWeatherController{
@Autowired
private ForecastWeatherService forecastWeatherService;
@GetMapping("/info/{counterValue}/info/{cityValue}")
public String getInfo(@PathVariable("counterValue") int counterValue, @PathVariable("cityValue") String cityValue , Model model){
String name = forecastWeatherService.getForecastInfoByCity(counterValue, cityValue).getCity().getName();
double temp = forecastWeatherService.getForecastInfoByCity(counterValue, cityValue).getList().listIterator().next().getMain().getTemp();
String desc = forecastWeatherService.getForecastInfoByCity(counterValue, cityValue).getList().listIterator().next().getWeather().listIterator().next().getDescription();
double minTemp = forecastWeatherService.getForecastInfoByCity(counterValue, cityValue).getList().listIterator().next().getMain().getTemp_min();
double maxTemp = forecastWeatherService.getForecastInfoByCity(counterValue, cityValue).getList().listIterator().next().getMain().getTemp_max();
String date = forecastWeatherService.getForecastInfoByCity(counterValue, cityValue).getList().listIterator().next().getDt_txt();
model.addAttribute("cityName", name);
model.addAttribute("weatherTemp", temp);
model.addAttribute("weatherDesc", desc);
model.addAttribute("weatherMinTemp", minTemp);
model.addAttribute("weatherMaxTemp", maxTemp);
model.addAttribute("weatherDate", date);
return "home2";
}
home2.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Weather API Application</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3 Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Weather Tracker API</h1>
<p>Search for the city you want to view weather details</p>
<table class="table">
<tr>
<th>
City</th>
<th>
Temperature Now</th>
<th>
Weather Description</th>
<th>
Minimum Temperature</th>
<th>
Maximum Temperature</th>
<th>Date</th>
</tr>
<td th:text="${cityName}">
<td th:text="${weatherTemp}">
<td th:text="${weatherDesc}">
<td th:text="${weatherMinTemp}">
<td th:text="${weatherMaxTemp}">
<td th:text="${weatherDate}"/>
</table>
</body>
</html>
ForecastWeatherService.java
public interface ForecastWeatherService {
public ForecastWeatherModel getForecastInfoByCity(int counter , String cityValue);
}
ForecastWeatherServiceImpl.java
@Service
public class ForecastWeatherServiceImpl implements ForecastWeatherService{
@Value("${forecast.url}")
private String forecastApiBaseUrl;
@Value("${additional.url}")
private String forecastApiAdditionalUrl;
@Value("${api.key}")
private String apiKey;
@Autowired
private RestTemplate restTemplate;
@Override
public ForecastWeatherModel getForecastInfoByCity(int counter , String cityValue) {
ForecastWeatherModel result = restTemplate.getForObject(forecastApiBaseUrl counter "&appid=" apiKey forecastApiAdditionalUrl cityValue "&units=metric" , ForecastWeatherModel.class);
return result;
}
應用程式屬性
external.api.url= https://api.openweathermap.org/data/2.5/weather?q=
api.key = ApiKeyExample
server.port = 8080
# ---------------------------FORECAST---------------------------
forecast.url = https://api.openweathermap.org/data/2.5/forecast?cnt=
additional.url = &q=
我是否必須在 getInfo 方法和 home2.html 中回圈,或者我應該制作一個 ForecastWeatherModel 串列并回圈元素?
uj5u.com熱心網友回復:
不要把每個屬性都放在上面,而是把整個物件放在上面。然后你可以使用 Thymeleaf 回圈遍歷。例如:
@GetMapping("/info/{counterValue}/info/{cityValue}")
public String getInfo(@PathVariable("counterValue") int counterValue, @PathVariable("cityValue") String cityValue , Model model){
model.addAttribute("forecast", forecastWeatherService.getForecastInfoByCity(counterValue, cityValue));
return "home2";
}
然后,在 Thymeleaf 中回圈相同的屬性。
<div class="container">
<h1>Weather Tracker API</h1>
<h2 th:text="${forecast.city}" />
<table class="table">
<tr>
<th>Temperature Now</th>
<th>Minimum Temperature</th>
<th>Maximum Temperature</th>
<th>Date</th>
</tr>
<tr th:each="data: ${forecast.list}">
<td th:text="${data.main.temp}" />
<td th:text="${data.main.getTemp_min()}" />
<td th:text="${data.main.getTemp_max()}" />
<td th:text="${data.main.getDt_txt()}" />
</tr>
</table>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/511703.html
