도로명주소 우편번호 조회서비스 & 지번주소 우편번호 조회서비스
공공 데이터 포탈 회원가입 후 서비스 신청 후에 이용가능하다.
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¤tPage=”);
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¤tPage=”);
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¤tPage=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¤tPage=1