Java

[Java] OPENAPI 크롤링/ json 파싱해서 DB에 저장하기

데메즈 2022. 7. 6. 18:14
728x90
반응형

 

@RequestMapping(value = "/rivInfo.do", method = RequestMethod.GET)
public ModelAndView rivInfoModelAndView(HttpSession session, HttpServletRequest request
        , HttpServletResponse response) throws Exception {

    ModelAndView mv = new ModelAndView("sample/rivInfo");

    List<EgovMap> lists = RivInfoService.selectWlobsifList();
    for(EgovMap list : lists) {
        String obscd = (String) list.get("wlobscd");
        Crawling(obscd);
    }
    //Crawling("1001602");

    return mv;
}
public void Crawling(String wlobscd) {

        try {
            Crawler crawler = new Crawler();
            String url = URLEncoder.encode(wlobscd, "UTF-8");

            rivInfoVO paramVO = new rivInfoVO();
            paramVO.setWlobscd(wlobscd);

            String _response = crawler.search(url);

            String[] fields = {"ymdh", "wl"};
            Map<String, Object> result = crawler.getResult(_response, fields);

            List<Map<String, Object>> items = (List<Map<String, Object>>) result.get("result");
            for(Map<String, Object> item : items) {
                for(String field : fields) {
                    if(field.equals("ymdh")){
                        paramVO.setYmdh((String) item.get(field));
                    } else {
                        paramVO.setWl((String) item.get(field));
                    }
                }
                boolean isSuccess = RivInfoService.insertWlhrInfo(paramVO);
            }

        } catch (Exception e){
            e.printStackTrace();;
        }

    }
public class Crawler{

        final String baseUrl = "http://www.wamis.go.kr:8080/wamis/openapi/wkw/wl_hrdata?startdt=20200101&enddt=20201231&obscd=";

        public String search(String _url){

            HttpURLConnection con = null;
            String result = "";

            try {
                URL url = new URL(baseUrl + _url);
                con = (HttpURLConnection) url.openConnection();

                con.setRequestMethod("GET");

                int responseCode = con.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) result = readBody(con.getInputStream());
                else result = readBody(con.getErrorStream());

            } catch (Exception e) {
                System.out.println("연결 오류 : " + e);
            } finally {
                con.disconnect();
            }

            return result;
        }

        public String readBody(InputStream body){
            InputStreamReader streamReader = new InputStreamReader(body);

            try (BufferedReader lineReader = new BufferedReader(streamReader)) {
                StringBuilder responseBody = new StringBuilder();

                String line;
                while ((line = lineReader.readLine()) != null) {
                    responseBody.append(line);
                }

                return responseBody.toString();
            } catch (IOException e) {
                throw new RuntimeException("API 응답을 읽는데 실패했습니다.", e);
            }
        }

        public Map<String, Object> getResult(String response, String[] fields) {
            Map<String, Object> rtnObj = new HashMap<> ();

            try {
                JSONParser parser = new JSONParser();
                JSONObject result = (JSONObject) parser.parse(response);

                JSONArray items = (JSONArray) result.get("list");
                List<Map<String, Object>> itemList = new ArrayList<>();

                for(int i = 0; i < items.size(); i++) {
                    JSONObject item = (JSONObject) items.get(i);
                    Map<String, Object> itemMap = new HashMap<>();

                    for(String field : fields) {
                        itemMap.put(field, item.get(field));
                    }
                    itemList.add(itemMap);
                }

                rtnObj.put("result", itemList);
            } catch (Exception e) {
                System.out.println("getResult error -> " + "파싱 실패, " + e.getMessage());
            }

            return rtnObj;
        }

    }
 

[Java] 네이버 검색 API 등록 및 호출하기

네이버 검색 API 등록 모든 API가 그러하지만 네이버 검색도 사용하기 위해서는 우선적으로 API 등록해서 키를 발급받아야 한다. developers.naver.com/docs/search/blog/ 검색 API 블로그 검색 개발가이드 NAVER

needjarvis.tistory.com

 

[Java] 네이버(Naver) 검색 사용 및 JSON 파싱(Parsing)하기

네이버 API 등록을 하는 것은 바로 이전 포스팅을 참고하면 되며 [Java] 네이버 검색 API 등록 및 호출하기 [Java] 네이버 검색 API 등록 및 호출하기 네이버 검색 API 등록 모든 API가 그러하지만 네이버

needjarvis.tistory.com

728x90
반응형