登录
原创

SpringBoot对接空气质量接口

发布于 2022-08-10 阅读 1351
  • 后端
  • Java
  • 程序员
  • 开源
原创

1、yaml

# 空气质量请求key
airqualityconfig:
  airqualityKey: 你自己的key

2、URL工具类

package com.kunya.kbd.platform.airquality.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * Project: 河北坤亚科技
 * Author: Java-zhouqichao
 * Tel:17600689790
 * Description:空气质量请求
 */
public class UrlUtils {

    /**
     * 获取url网址返回的数据内容
     *
     * @param urlStr
     * @return
     */
    public static String loadURL(String urlStr) {
        try {
            URL url = new URL(urlStr);
            HttpURLConnection urlConnection =
                    (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();
            String responseStr = ConvertToString(inputStream);
            return responseStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}

3、创建返回前端的DTO

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * Project: 河北坤亚科技
 * Author: Java-zhouqichao
 * Tel:17600689790
 * Description:空气质量请求
 */
@NoArgsConstructor
@Data
public class AirQualityDTO implements Serializable {

    @JsonProperty("resultcode")
    private String resultcode;
    @JsonProperty("reason")
    private String reason;
    @JsonProperty("error_code")
    private Integer errorCode;
    @JsonProperty("result")
    private List<ResultDTO> result;

    @NoArgsConstructor
    @Data
    public static class ResultDTO implements Serializable  {
        @JsonProperty("citynow")
        private CitynowDTO citynow;
        @JsonProperty("lastTwoWeeks")
        private LastTwoWeeksDTO lastTwoWeeks;
        @JsonProperty("lastMoniData")
        private LastMoniDataDTO lastMoniData;

        @NoArgsConstructor
        @Data
        public static class CitynowDTO implements Serializable  {
            @JsonProperty("city")
            private String city;
            @JsonProperty("AQI")
            private String aqi;
            @JsonProperty("quality")
            private String quality;
            @JsonProperty("date")
            private String date;
        }

        @NoArgsConstructor
        @Data
        public static class LastTwoWeeksDTO implements Serializable  {
            @JsonProperty("1")
            private _$1DTO $1;
            @JsonProperty("2")
            private _$2DTO $2;
            @JsonProperty("3")
            private _$3DTO $3;
            @JsonProperty("4")
            private _$4DTO $4;
            @JsonProperty("5")
            private _$5DTO $5;
            @JsonProperty("6")
            private _$6DTO $6;
            @JsonProperty("7")
            private _$7DTO $7;
            @JsonProperty("8")
            private _$8DTO $8;
            @JsonProperty("9")
            private _$9DTO $9;
            @JsonProperty("10")
            private _$10DTO $10;
            @JsonProperty("11")
            private _$11DTO $11;
            @JsonProperty("12")
            private _$12DTO $12;
            @JsonProperty("13")
            private _$13DTO $13;
            @JsonProperty("14")
            private _$14DTO $14;
            @JsonProperty("15")
            private _$15DTO $15;
            @JsonProperty("16")
            private _$16DTO $16;
            @JsonProperty("17")
            private _$17DTO $17;

            @NoArgsConstructor
            @Data
            public static class _$1DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$2DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$3DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$4DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$5DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$6DTO  implements Serializable {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$7DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$8DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$9DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$10DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$11DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$12DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$13DTO  implements Serializable {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$14DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$15DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$16DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }

            @NoArgsConstructor
            @Data
            public static class _$17DTO  implements Serializable {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("date")
                private String date;
            }
        }

        @NoArgsConstructor
        @Data
        public static class LastMoniDataDTO implements Serializable  {
            @JsonProperty("1")
            private _$1DTO $1;
            @JsonProperty("2")
            private _$2DTO $2;
            @JsonProperty("3")
            private _$3DTO $3;
            @JsonProperty("4")
            private _$4DTO $4;
            @JsonProperty("5")
            private _$5DTO $5;
            @JsonProperty("6")
            private _$6DTO $6;
            @JsonProperty("7")
            private _$7DTO $7;

            @NoArgsConstructor
            @Data
            public static class _$1DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("PM2.5Hour")
                private String _$PM25Hour286;// FIXME check this code
                @JsonProperty("PM2.5Day")
                private String _$PM25Day127;// FIXME check this code
                @JsonProperty("PM10Hour")
                private String pM10Hour;
                @JsonProperty("lat")
                private String lat;
                @JsonProperty("lon")
                private String lon;
            }

            @NoArgsConstructor
            @Data
            public static class _$2DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("PM2.5Hour")
                private String _$PM25Hour222;// FIXME check this code
                @JsonProperty("PM2.5Day")
                private String _$PM25Day16;// FIXME check this code
                @JsonProperty("PM10Hour")
                private String pM10Hour;
                @JsonProperty("lat")
                private String lat;
                @JsonProperty("lon")
                private String lon;
            }

            @NoArgsConstructor
            @Data
            public static class _$3DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("PM2.5Hour")
                private String _$PM25Hour315;// FIXME check this code
                @JsonProperty("PM2.5Day")
                private String _$PM25Day104;// FIXME check this code
                @JsonProperty("PM10Hour")
                private String pM10Hour;
                @JsonProperty("lat")
                private String lat;
                @JsonProperty("lon")
                private String lon;
            }

            @NoArgsConstructor
            @Data
            public static class _$4DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("PM2.5Hour")
                private String _$PM25Hour10;// FIXME check this code
                @JsonProperty("PM2.5Day")
                private String _$PM25Day123;// FIXME check this code
                @JsonProperty("PM10Hour")
                private String pM10Hour;
                @JsonProperty("lat")
                private String lat;
                @JsonProperty("lon")
                private String lon;
            }

            @NoArgsConstructor
            @Data
            public static class _$5DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("PM2.5Hour")
                private String _$PM25Hour290;// FIXME check this code
                @JsonProperty("PM2.5Day")
                private String _$PM25Day2;// FIXME check this code
                @JsonProperty("PM10Hour")
                private String pM10Hour;
                @JsonProperty("lat")
                private String lat;
                @JsonProperty("lon")
                private String lon;
            }

            @NoArgsConstructor
            @Data
            public static class _$6DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("PM2.5Hour")
                private String _$PM25Hour290;// FIXME check this code
                @JsonProperty("PM2.5Day")
                private String _$PM25Day30;// FIXME check this code
                @JsonProperty("PM10Hour")
                private String pM10Hour;
                @JsonProperty("lat")
                private String lat;
                @JsonProperty("lon")
                private String lon;
            }

            @NoArgsConstructor
            @Data
            public static class _$7DTO implements Serializable  {
                @JsonProperty("city")
                private String city;
                @JsonProperty("AQI")
                private String aqi;
                @JsonProperty("quality")
                private String quality;
                @JsonProperty("PM2.5Hour")
                private String _$PM25Hour279;// FIXME check this code
                @JsonProperty("PM2.5Day")
                private String _$PM25Day143;// FIXME check this code
                @JsonProperty("PM10Hour")
                private String pM10Hour;
                @JsonProperty("lat")
                private String lat;
                @JsonProperty("lon")
                private String lon;
            }
        }
    }
}

4、Service

import com.alibaba.fastjson.JSON;
import com.kunya.kbd.common.utils.MapUtil;
import com.kunya.kbd.platform.Constants;
import com.kunya.kbd.platform.airquality.util.UrlUtils;
import com.kunya.kbd.platform.airquality.vo.AirQualityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Project: 河北坤亚科技
 * Author: Java-zhouqichao
 * Tel:17600689790
 * Description:空气质量请求
 */
@Slf4j
@Service
public class AirQualityService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${airqualityconfig.airqualityKey}")
    private String airqualityKey;

    public Map<String, Object> reqAirQuality(String cityName) throws UnsupportedEncodingException {

        StringBuilder requestUrl = new StringBuilder("http://web.juhe.cn/environment/air/cityair?");
        requestUrl.append("city=" + URLEncoder.encode(cityName,"utf-8"));
        requestUrl.append("&key=" + airqualityKey);

        String responseUrl = UrlUtils.loadURL(requestUrl.toString());
        AirQualityVO airQualityVO = JSON.parseObject(responseUrl, AirQualityVO.class);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("responseUrl",responseUrl);
        resultMap.put("airQualityVO",airQualityVO);
        redisUtil.hmset(redisKey, resultMap, Constants.AirQuality_EXPIRE_SECOND);// 常量自定,不提供
        return resultMap;
    }
}

5、Controller

package com.kunya.kbd.platform.airquality.controller;

import com.kunya.kbd.common.exception.BusinessRuntimeException;
import com.kunya.kbd.common.http.HttpResult;
import com.kunya.kbd.platform.airquality.service.AirQualityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Project: 河北坤亚科技
 * Author: Java-zhouqichao
 * Tel:17600689790
 * Description:空气质量请求
 */
@Slf4j
@RestController
@RequestMapping(value = "airQuality")
@Api(tags = "第三方天气和空气请求接口")
public class AirQualityController {

    @Autowired
    private AirQualityService airQualityService;

    @ApiOperation(value = "空气质量接口")
    @GetMapping("/reqAirQuality")
    public HttpResult reqAirQuality(@RequestParam("cityName") String cityName){
        try {
            Map<String,Object> result = airQualityService.reqAirQuality(cityName);
            return HttpResult.success("请求空气质量接口成功", result);
        } catch (BusinessRuntimeException e) {
            log.error(e.getMessage());
            return HttpResult.error(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return HttpResult.error("请求空气质量接口失败");
        }
    }

}

资源分享 分享资源来自用户上传,若用户上传的内容存在权利纠纷或涉嫌侵权,请联系我们及时删除
下载链接 提取码 下载
下载链接 sai9 复制 前往下载

评论区

励志做一条安静的咸鱼,从此走上人生巅峰。

资源分享

2

0

0

举报