생활정보ZIP

도로명주소 우편번호 조회서비스 & 지번주소 우편번호 조회서비스

공공 데이터 포탈 회원가입 후 서비스 신청 후에 이용가능하다.

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import platform.rest.common.config.ServiceConfig;
import platform.rest.common.exception.BizException;
import platform.rest.common.util.HttpUtil;

@Component
public class AddressUtil {
@Autowired ServiceConfig serviceConfig;
public List<AddressInfo> getOldAddress(String searchWord, String currentPage) throws BizException {
String serviceKey = serviceConfig.getAddressServiceKey();
String oldAddressUrl = serviceConfig.getAddressOldUrl();

// using run as java application
//String serviceKey =”0lFHSExSL5A N7Jud7eY8CdZa1dAus05LcoPtb78svQYZMOnwTjZlfdcg%3D%3D”;
//String oldAddressUrl = “http://openapi.epost.go.kr/postal/retrieveLotNumberAdressAreaCdService/retrieveLotNumberAdressAreaCdService/getDetailListAreaCd?”;

List<AddressInfo> addressList = new ArrayList<AddressInfo>();
try {

StringBuffer url = new StringBuffer();
url.append(oldAddressUrl);
url.append(“serviceKey=”);
//url.append(URLEncoder.encode(serviceKey, “UTF-8”));  이미 인코딩 된 값으로 가져옴으로 변경한다.
url.append(serviceKey);
url.append(“&”);
url.append(“searchSe=dong&”);
url.append(“srchwrd=”);
url.append(URLEncoder.encode(searchWord, “UTF-8”));
url.append(“&countPerPage=20&currentPage=”);
url.append(currentPage);
String xml = HttpUtil.get(url.toString());

//헤더정보 체크 
Object headerObject =  XML.toJSONObject(xml).getJSONObject(“DetailListResponse”).opt(“cmmMsgHeader”);  
JSONObject  jsonObjectForheader = (JSONObject) headerObject;
String totalPageCnt = jsonObjectForheader.getString(“totalPage”);

Object object =  XML.toJSONObject(xml).getJSONObject(“DetailListResponse”).opt(“detailListAreaCd”);
if(object instanceof JSONObject) {
JSONObject  jsonObject = (JSONObject) object;
AddressInfo addressInfo = new AddressInfo();
addressInfo.setAddress(jsonObject.getString(“adres”));
addressInfo.setZipCode(jsonObject.getString(“zipNo”));
addressInfo.setTotalPage(totalPageCnt);
addressList.add(addressInfo);
} else if(object instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) object;
for(int i = 0;i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
AddressInfo addressInfo = new AddressInfo();
addressInfo.setAddress(jsonObject.getString(“adres”));
addressInfo.setZipCode(jsonObject.getString(“zipNo”));
addressInfo.setTotalPage(totalPageCnt);
addressList.add(addressInfo);
}
}
} catch(Exception e) {
throw new BizException(“BZ01”, “데이터 검색에 실패했습니다.”);

return addressList;
}

public List<AddressInfo> getNewAddress(String searchWord, String currentPage) throws BizException {

String serviceKey = serviceConfig.getAddressServiceKey();
String newAddressUrl = serviceConfig.getAddressNewUrl();

// using run as java application
//String serviceKey =”0lFHSExSL5AtA71%2FqG19fjtcuQiB9LrW Aus05LcoPtb78svQYZMOnwTjZlfdcg%3D%3D”;
//String newAddressUrl = “http://openapi.epost.go.kr/postal/retrieveNewAdressAreaCdService/retrieveNewAdressAreaCdService/getNewAddressListAreaCd?”;

List<AddressInfo> addressList = new ArrayList<AddressInfo>();
try {
StringBuffer url = new StringBuffer();
url.append(newAddressUrl);
url.append(“serviceKey=”);
//url.append(URLEncoder.encode(serviceKey, “UTF-8”)); 2018.11.26 이미 인코딩 된 값으로 가져옴으로 변경한다.
url.append(serviceKey);
url.append(“&”);
url.append(“searchSe=road&”);
url.append(“srchwrd=”);
url.append(URLEncoder.encode(searchWord, “UTF-8”));
url.append(“&countPerPage=20&currentPage=”);
url.append(currentPage);
System.out.println(url.toString());
String xml = HttpUtil.get(url.toString());
System.out.println(XML.toJSONObject(xml).toString());

// //헤더정보 체크 
Object headerObject =  XML.toJSONObject(xml).getJSONObject(“NewAddressListResponse”).opt(“cmmMsgHeader”);  
JSONObject  jsonObjectForheader = (JSONObject) headerObject;
String totalPageCnt = jsonObjectForheader.getString(“totalPage”);

Object object = XML.toJSONObject(xml).getJSONObject(“NewAddressListResponse”).opt(“newAddressListAreaCd”);
if(object instanceof JSONObject) {
JSONObject  jsonObject = (JSONObject) object;
AddressInfo addressInfo = new AddressInfo();
addressInfo.setAddress(jsonObject.getString(“lnmAdres”)); addressInfo.setZipCode(jsonObject.getString(“zipNo”));
addressInfo.setTotalPage(totalPageCnt);
addressList.add(addressInfo);
} else if(object instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) object;
for(int i = 0;i<jsonArray.length();i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
AddressInfo addressInfo = new AddressInfo(); addressInfo.setAddress(jsonObject.getString(“lnmAdres”)); addressInfo.setZipCode(jsonObject.getString(“zipNo”)); addressInfo.setTotalPage(totalPageCnt);
addressList.add(addressInfo);
}

}

} catch(Exception e) {
throw new BizException(“BZ01”, “데이터 검색에 실패했습니다.”);

return addressList;
}

public static void main(String[] args) {
AddressUtil util = new AddressUtil();
try {

List<AddressInfo> oldList;
oldList = util.getOldAddress(“수성동”, “1”);
for(int i = 0;i<oldList.size();i++) {
System.out.println(oldList.get(i).toString());

}

// List<AddressInfo> newList = util.getNewAddress(“충정로 380”);
// List<AddressInfo> newList = AddressUtil.getNewAddress(“디지털로 33길”, “1”);
// for(int i = 0;i<newList.size();i++) {
// System.out.println(newList.get(i).toString());
// }
} catch (BizException e) {
                        // TODO Auto-generated catch block
e.printStackTrace();
}
}
}

공공 데이터 포탈 : https://www.data.go.kr

도로명주소 우편번호 조회서비스 & 지번주소 우편번호 조회서비스 데이터 받아서 처리하는 로직이다.

http://openapi.epost.go.kr/postal/retrieveNewAdressAreaCdService/retrieveNewAdressAreaCdService/getNewAddressListAreaCd?serviceKey=0lFHSExSL5At Z51%2FanqbN7Jud7eY8CdZa1dAus05LcoPtb78svQYZMOnwTjZlfdcg%3D%3D&searchSe=road&srchwrd=%EC%95%94%EC%82%AC%EA%B8%B835&countPerPage=10&currentPage=1

http://openapi.epost.go.kr/postal/retrieveLotNumberAdressAreaCdService/retrieveLotNumberAdressAreaCdService/getDetailListAreaCd?serviceKey=0lFHSExSLjtcuQiB9LrWGZ51%2FanqbN7Jud7eY8CdZa1dAus05LcoPtb78svQYZMOnwTjZlfdcg%3D%3D&searchSe=dong&srchwrd=%EC%88%98%EC%84%B1%EB%8F%99&countPerPage=20&currentPage=1

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<DetailListResponse>
<cmmMsgHeader>
<requestMsgId/>
<responseMsgId/>
<responseTime>20181128:175038932</responseTime>
<successYN>Y</successYN>
<returnCode>00</returnCode>
<errMsg/>
<totalCount>127</totalCount>
<countPerPage>50</countPerPage>
<totalPage>3</totalPage>
<currentPage>1</currentPage>
</cmmMsgHeader>
<detailListAreaCd>
<no>1</no>
<zipNo>06123</zipNo>
<adres>서울특별시 강남구 역삼동 601~601-31</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>2</no>
<zipNo>06124</zipNo>
<adres>서울특별시 강남구 역삼동 602~603-12</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>3</no>
<zipNo>06125</zipNo>
<adres>서울특별시 강남구 역삼동 604~606-33</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>4</no>
<zipNo>06126</zipNo>
<adres>서울특별시 강남구 역삼동 607~609-27</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>5</no>
<zipNo>06127</zipNo>
<adres>서울특별시 강남구 역삼동 610~614-25</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>6</no>
<zipNo>06124</zipNo>
<adres>서울특별시 강남구 역삼동 615-2~617-6</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>7</no>
<zipNo>06128</zipNo>
<adres>서울특별시 강남구 역삼동 618~619-14</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>8</no>
<zipNo>06129</zipNo>
<adres>서울특별시 강남구 역삼동 619-15~620-24</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>9</no>
<zipNo>06128</zipNo>
<adres>서울특별시 강남구 역삼동 621-2~621-13</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>10</no>
<zipNo>06129</zipNo>
<adres>서울특별시 강남구 역삼동 621-14~621-34</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>11</no>
<zipNo>06128</zipNo>
<adres>서울특별시 강남구 역삼동 621-36~622-27</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>12</no>
<zipNo>06127</zipNo>
<adres>서울특별시 강남구 역삼동 623~624-45</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>13</no>
<zipNo>06126</zipNo>
<adres>서울특별시 강남구 역삼동 625~630-13</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>14</no>
<zipNo>06131</zipNo>
<adres>서울특별시 강남구 역삼동 631~632-20</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>15</no>
<zipNo>06130</zipNo>
<adres>서울특별시 강남구 역삼동 633~636-33</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>16</no>
<zipNo>06131</zipNo>
<adres>서울특별시 강남구 역삼동 637~641-21</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>17</no>
<zipNo>06132</zipNo>
<adres>서울특별시 강남구 역삼동 642~642-24</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>18</no>
<zipNo>06131</zipNo>
<adres>서울특별시 강남구 역삼동 643~645-30</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>19</no>
<zipNo>06132</zipNo>
<adres>서울특별시 강남구 역삼동 646~646-22</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>20</no>
<zipNo>06133</zipNo>
<adres>서울특별시 강남구 역삼동 647~648-26</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>21</no>
<zipNo>06134</zipNo>
<adres>서울특별시 강남구 역삼동 649~649-14</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>22</no>
<zipNo>06135</zipNo>
<adres>서울특별시 강남구 역삼동 650~653-5</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>23</no>
<zipNo>06138</zipNo>
<adres>서울특별시 강남구 역삼동 654-3~654-6</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>24</no>
<zipNo>06137</zipNo>
<adres>서울특별시 강남구 역삼동 655~656-38</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>25</no>
<zipNo>06136</zipNo>
<adres>서울특별시 강남구 역삼동 657~660-38</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>26</no>
<zipNo>06137</zipNo>
<adres>서울특별시 강남구 역삼동 661~661-35</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>27</no>
<zipNo>06138</zipNo>
<adres>서울특별시 강남구 역삼동 662~662-18</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>28</no>
<zipNo>06137</zipNo>
<adres>서울특별시 강남구 역삼동 663~664-44</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>29</no>
<zipNo>06136</zipNo>
<adres>서울특별시 강남구 역삼동 665-1~667-19</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>30</no>
<zipNo>06140</zipNo>
<adres>서울특별시 강남구 역삼동 668~671-34</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>31</no>
<zipNo>06139</zipNo>
<adres>서울특별시 강남구 역삼동 672~675-3</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>32</no>
<zipNo>06142</zipNo>
<adres>서울특별시 강남구 역삼동 677~677-35</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>33</no>
<zipNo>06141</zipNo>
<adres>서울특별시 강남구 역삼동 678~679-5</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>34</no>
<zipNo>06143</zipNo>
<adres>서울특별시 강남구 역삼동 680~682-27</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>35</no>
<zipNo>06145</zipNo>
<adres>서울특별시 강남구 역삼동 683~683-43</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>36</no>
<zipNo>06144</zipNo>
<adres>서울특별시 강남구 역삼동 684~688-6</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>37</no>
<zipNo>06147</zipNo>
<adres>서울특별시 강남구 역삼동 689~689-34</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>38</no>
<zipNo>06146</zipNo>
<adres>서울특별시 강남구 역삼동 690~692-26</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>39</no>
<zipNo>06145</zipNo>
<adres>서울특별시 강남구 역삼동 693~693-36</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>40</no>
<zipNo>06149</zipNo>
<adres>서울특별시 강남구 역삼동 694~696-50</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>41</no>
<zipNo>06148</zipNo>
<adres>서울특별시 강남구 역삼동 697~697-64</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>42</no>
<zipNo>06146</zipNo>
<adres>서울특별시 강남구 역삼동 698~698-39</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>43</no>
<zipNo>06147</zipNo>
<adres>서울특별시 강남구 역삼동 699~700-35</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>44</no>
<zipNo>06152</zipNo>
<adres>서울특별시 강남구 역삼동 701~701-10</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>45</no>
<zipNo>06151</zipNo>
<adres>서울특별시 강남구 역삼동 702~703-9</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>46</no>
<zipNo>06148</zipNo>
<adres>서울특별시 강남구 역삼동 704~704-68</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>47</no>
<zipNo>06150</zipNo>
<adres>서울특별시 강남구 역삼동 705~705-32</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>48</no>
<zipNo>06210</zipNo>
<adres>서울특별시 강남구 역삼동 706~706-26</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>49</no>
<zipNo>06211</zipNo>
<adres>서울특별시 강남구 역삼동 707-1~707-42</adres>
</detailListAreaCd>
<detailListAreaCd>
<no>50</no>
<zipNo>06212</zipNo>
<adres>서울특별시 강남구 역삼동 708~708-41</adres>
</detailListAreaCd>
</DetailListResponse>

Leave a Reply

error: Content is protected !!