농업기상정보 서비스
농업 관련 기상 데이터가 필요하게 되어 공공데이터 포털 사이트의 농업기상정보 API를 사용하여 조회하였다.
Open API 활용 방법
- 농업기상정보서비스 사이트에 접속한다.
- 이용방법에 따라 회원가입 후 Open API 신청을 하고 Service Key를 발급받는다.
- 활용 가이드를 다운로드하여 조회 조건 및 결과 데이터를 이해한다.
농업 기상 데이터 조회
1
| http://weather.rda.go.kr/openapi/realtime_openapi_xml.jsp?mberid={id}®ist_ky={발급받은 Service Key}
|
결과 데이터(XML 형식)
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Root> <Info> <stncode>380959A001</stncode> <stnname>충주시 달천동</stnname> <date>2020/07/31 14:50</date> <temp_150>28.3℃</temp_150> <tmprt_150Top>29.7℃</tmprt_150Top> <tmprt_150Lwet>22.5℃</tmprt_150Lwet> <tmprt_50>-℃</tmprt_50> <tmprt_50Top>-℃</tmprt_50Top> <tmprt_50Lwet>-℃</tmprt_50Lwet> <tmprt_400>-℃</tmprt_400> <tmprt_400Top>-℃</tmprt_400Top> <tmprt_400Lwet>-℃</tmprt_400Lwet> <hd_150>86.2%</hd_150> <hd_150Top>100.0%</hd_150Top> <hd_150Lwet>75.6%</hd_150Lwet> <hd_50>-%</hd_50> <hd_50Top>-%</hd_50Top> <hd_50Lwet>-%</hd_50Lwet> <hd_400>-%</hd_400> <hd_400Top>-%</hd_400Top> <hd_400Lwet>-%</hd_400Lwet> <wd_300>남동</wd_300> <wd_300Top>남</wd_300Top> <wd_300Lwet>정온</wd_300Lwet> <wd_150>-</wd_150> <wd_150Top>-</wd_150Top> <wd_150Lwet>-</wd_150Lwet> <wd_1000>-</wd_1000> <wd_1000Top>-</wd_1000Top> <wd_1000Lwet>-</wd_1000Lwet> <arvlty_300>1.9</arvlty_300> <arvlty_300Top>4.0</arvlty_300Top> <arvlty_300Lwet>0.0</arvlty_300Lwet> <arvlty_150>-</arvlty_150> <arvlty_150Top>-</arvlty_150Top> <arvlty_150Lwet>-</arvlty_150Lwet> <arvlty_1000>-</arvlty_1000> <arvlty_1000Top>-</arvlty_1000Top> <arvlty_1000Lwet>-</arvlty_1000Lwet> <afp>0.0mm</afp> <afv>-mm</afv> <sunshnTime>202.0hr</sunshnTime> <solradQy>10.4MJ/㎡</solradQy> <dwcnTime>0.0hr</dwcnTime> <pnwgTp>-hr</pnwgTp> <pnwgTpTop>-hr</pnwgTpTop> <pnwgTpLwet>-hr</pnwgTpLwet> <frfrTp>-℃</frfrTp> <frfrTpTop>-℃</frfrTpTop> <frfrTpLwet>-℃</frfrTpLwet> <udgrHeattCndctvt>-100.0℃</udgrHeattCndctvt> <udgrHeattCndctvtTop>-100.0℃</udgrHeattCndctvtTop> <udgrHeattCndctvtLwet>-100.0℃</udgrHeattCndctvtLwet> <udgrTp_10>-℃</udgrTp_10> <udgrTp_10Top>26.1℃</udgrTp_10Top> <udgrTp_10Lwet>24.4℃</udgrTp_10Lwet> <udgrTp_5>-℃</udgrTp_5> <udgrTp_5Top>-℃</udgrTp_5Top> <udgrTp_5Lwet>-℃</udgrTp_5Lwet> <udgrTp_20>-℃</udgrTp_20> <udgrTp_20Top>-℃</udgrTp_20Top> <udgrTp_20Lwet>-℃</udgrTp_20Lwet> <soilMitr_10>32.5</soilMitr_10> <soilMitr_10Top>36.3</soilMitr_10Top> <soilMitr_10Lwet>27.3</soilMitr_10Lwet> <soilMitr_10Cmst>23.4</soilMitr_10Cmst> <soilMitr_10CmstTop>27.2</soilMitr_10CmstTop> <soilMitr_10CmstLwet>18.2</soilMitr_10CmstLwet> <soilMitr_20>-</soilMitr_20> <soilMitr_20Top>-</soilMitr_20Top> <soilMitr_20Lwet>-</soilMitr_20Lwet> <soilMitr_20Cmst>-</soilMitr_20Cmst> <soilMitr_20CmstTop>-</soilMitr_20CmstTop> <soilMitr_20CmstLwet>-</soilMitr_20CmstLwet> <soilMitr_30>-</soilMitr_30> <soilMitr_30Top>-</soilMitr_30Top> <soilMitr_30Lwet>-</soilMitr_30Lwet> <soilMitr_30Cmst>-</soilMitr_30Cmst> <soilMitr_30CmstTop>-</soilMitr_30CmstTop> <soilMitr_30CmstLwet>-</soilMitr_30CmstLwet> </Info> </Root>
|
1) Respone Domain 생성
기본적으로 결과가 XML 형식이므로 javax.xml를 사용하여 도메인을 생성하였다.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import lombok.Data;
@Data @XmlRootElement(name = "Root") @XmlAccessorType(value = XmlAccessType.FIELD) public class ResponseXml {
@XmlElement(name = "Info") private Info info;
@Data public static class Info {
private String stncode;
private String stnname;
private String date;
private String temp_150;
private String hd_150;
private String wd_300;
private float arvlty_300;
private String afp;
private String afv;
private String solradQy;
private String sunshnTime;
private float soilMitr_10; } }
|
2) 데이터 조회
SpringFramework에서 지원하는 RestTemplate를 사용하여 조회한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| private final String BASE_URL = "http://weather.rda.go.kr/openapi/realtime_openapi_xml.jsp"; private final String userId = ""; private final String serviceKey = "";
public void getWeatherData() { StringBuilder urlBuilder = new StringBuilder(BASE_URL); try { urlBuilder.append("?" + URLEncoder.encode("mberid", "UTF-8") + "=" + userId); urlBuilder.append("&" + URLEncoder.encode("regist_ky", "UTF-8") + "=" + serviceKey);
RestTemplate restTemplate = new RestTemplate(); ResponseXml response = restTemplate.getForObject(urlBuilder.toString(), ResponseXml.class);
System.out.println(response); } catch (Exception e) { e.printStackTrace(); } }
|