달력

52024  이전 다음

  • 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

정부 에서 제공하는 공공데이터

지역코드(lawdcd) 와 년월(ymd)를 입력받아 지역의 매매 자료를 조회한다.

 

이후 KakaoChg 함수는 공공데이터에서 주소를 분리하여 좌표로 변환하는 역할을 가지고 있으나,

테스트로 작성된 코드여서 임시 값으로 테스트만 진행 하였다.


@RestController
public class DataController {

    @GetMapping("/test")
    public String callApiGate(String lawdcd, String ymd) {
        String jsonPrintString = null;
        String apiUrl = "";
       
        // 국토교통부_오피스텔 매매 신고 조회 서비스
        apiUrl = "http://openapi.molit.go.kr/OpenAPI_ToolInstallPackage/service/rest/RTMSOBJSvc/getRTMSDataSvcOffiTrade?"
                + "LAWD_CD=" + lawdcd + "&DEAL_YMD=" + ymd
                + "&serviceKey=******************************";
        // serviceKey 는 공공데이터 포털을 가입후 발급 신청 하여야 한다.
        jsonPrintString = callApiWithJson(apiUrl);
        
        // 받은 데이터 출력 
        System.out.println(jsonPrintString);
        
        // 수신한 공공데이터의 주소 부분한 잘라내어, 맵(카카오,네이버)에 표시 할 수 있는 좌표계로 변환.
        jsonPrintString = KakaoChg("강남대로 100");

        return jsonPrintString;
    }

    private static String GEOCODE_URL="http://dapi.kakao.com/v2/local/search/address.json?query=";
    
    // 카카오 개발자 센터 에서 api 키를 발급 받아야 하며, 아래와 같이 한칸 띄우고 사용한다.
    private static String GEOCODE_USER_INFO="KakaoAK *******************"; 
  
    private String KakaoChg(String args) {
      
        URL obj;
        String jsonPrintString = null;

        try{
            System.out.println(args);
            String address = URLEncoder.encode(args, "UTF-8");
            
            obj = new URL(GEOCODE_URL+address);
         
            HttpURLConnection con = (HttpURLConnection)obj.openConnection();
            
            con.setRequestMethod("GET");
            con.setRequestProperty("Authorization",GEOCODE_USER_INFO);
            con.setRequestProperty("content-type", "application/json");
            con.setDoOutput(true);
            con.setUseCaches(false);
            con.setDefaultUseCaches(false);
         
            Charset charset = Charset.forName("UTF-8");
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
            
            String inputLine;
            StringBuffer response = new StringBuffer();
            
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            JSONParser json= new JSONParser();
            org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject)json.parse(response.toString());
            
            jsonPrintString = jsonObject.toString();
      } catch (Exception e) {
         e.printStackTrace();
      }
        return jsonPrintString;
   }

    private String callApiWithJson(String apiUrl) {
        StringBuffer result = new StringBuffer();
        String jsonPrintString = null;
        try {
            URL url = new URL(apiUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream, "UTF-8"));
            String returnLine;
            while ((returnLine = bufferedReader.readLine()) != null) {
                result.append(returnLine);
            }

            JSONObject jsonObject = XML.toJSONObject(result.toString());
            jsonPrintString = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonPrintString;
    }
}

간단히 위와 같이 api 를 수신하여 결과를 확인 할 수 있다.

Posted by 무심법
|