双重预防项目-国泰新华二开定制版
SZH
2022-08-20 f9f0687195e0fe349185437d22c495d74c8d4a20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ruoyi.project.tool.weather.service;
 
 
 
 
import com.ruoyi.project.tool.weather.dto.WeatherDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
 
@Component
@Slf4j
public class WeatherManager {
    //请求连接地址
    final static String SOJSON_WEATHER_URL = "http://t.weather.itboy.net/api/weather/city/{1}";
 
    /**
     * 获取数据
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "weather_cache", key = "#id")// 从缓存获取,key为ID,缓存具体看 ehcache.xml 配置文件
    public WeatherDto getById(String id) {
        log.info("WeatherManager#getById: id={}", id);
 
        try {
            RestTemplate restTemplate = new RestTemplate();
            WeatherDto dto = restTemplate.getForObject(SOJSON_WEATHER_URL , WeatherDto.class,id);
 
            if(dto != null && dto.isSuccess()){
                return dto;
            }else{
                log.error("获取天气数据返回错误:{}", dto);
            }
        } catch (RestClientException e) {
            log.error("获取天气数据返回错误,出现异常.", e);
        }
        return null;
    }
 
}