-
[Java] ๊ณต๊ณต๋ฐ์ดํฐํฌํธ Open API ํ์ฑ, JPA๐ธ 2024. 8. 8. 00:02
๊ฐ๋ฐ ์ค์ ๊ณต๊ณต๋ฐ์ดํฐํฌํธ Open API ์๋น์ค๋ฅผ ์ฐ๊ฒฐํด์ ์ฌ์ฉํ ์ผ์ด ์์ด์ ์๋ฃ ์ฐพ์๋ณด๊ณ ์ฝ๋ ์์ฑํ ๊น์ ์ ๋ฆฌ! ์๋ฌด๋๋ ๊ฐ์ธ ํ๋ก์ ํธ ํ๊ฑฐ๋ ํ๋ฉด ์ข ์ข ํ์ฉํ ์ผ์ด ์์ง ์์๊น ์ถ๋ค.
1. ๊ณต๊ณต๋ฐ์ดํฐํฌํธ์์ ํ์ํ Open API ํ์ฉ ์ ์ฒญ
2. ํ์ฉ ์ ์ฒญํ๊ณ ์ ๋ณด ํ์ธ
- API ์์ธ ํ์ด์ง์์ ์๋ํ๋ ์ธ์ฆํค(๋ณธ์ธ์ ์น ํ์ด์ง ๋ด์์๋ Decoding, ๊ฐ๋ฐ ํ๊ฒฝ์์๋ Encoding ์ธ์ฆํค๊ฐ ์๋ํ๋ค.) ์ ๋ ฅํ๊ณ ์คํํ๋ฉด ๊ฒฐ๊ณผ json ๊ฐ์ ํ์ธํ ์ ์๋ค.
- ์ฝ๋๋ชฉ๋ก์ ๊ฒฝ์ฐ [์ฐธ๊ณ ๋ฌธ์ - ์ฝ๋ ์ ์์]์์ ์์ธ ๋ด์ฉ์ ํ์ธํ ์ ์๋ค.
3. ์๋ฃ ํ์ฉ
๋ณธ์ธ์ ๊ฒฝ์ฐ์๋ ๋ค์๊ณผ ๊ฐ์ด ํ์ฉํ์๋ค. ์ง๊ธ์ ์๋์ผ๋ก ํธ์ถํด์ผ ํ๋ ์ํ๊ธด ํ๋ฐ, ํ์ ๋ฐฐ์น ์์ ์ ์ถ๊ฐํ๋ฉด ์ข ๋ ํธํ๊ณ ์ค์๊ฐ์ผ๋ก ๋ด์ฉ์ ๋ฐ์ํ ์ ์์ ๊ฒ ๊ฐ๋ค! ์คํ๋ง์์๋ ์ค์ผ์ค๋ฌ๋ผ๊ณ ํ๋(..) ์๋ฌดํผ
- ํด๋น API ๋ฐ์ดํฐ๋ฅผ JSON์ผ๋ก ๋ฐ์์ค๋ ํด๋์ค๋ฅผ ์์ฑ
- Service๋จ์์ ์ด๋ฏธ ์ธํ ํด๋์ ๊ฐ์ฒด ์ํฐํฐ์ ๊ฐ๊ณตํด์ DB์ save
- Controller์์ Post ํธ์ถํด์ ์๋น์ค ํธ์ถ
RecruitAPI
public class RecruitAPI { public JSONObject getRecruitData() throws Exception { StringBuilder urlBuilder = new StringBuilder("API BASE URL/ํ์ฉํ๋ REST API"); urlBuilder.append("?" + URLEncoder.encode("ServiceKey", "UTF-8") + "์ธ์ฆํค ์ ๋ ฅ"); // ๋ถ๋ฌ์ค๋ ๋ฐ์ดํฐ ๊ฐ์ ์ง์ urlBuilder.append("&" + URLEncoder.encode("numOfRows", "UTF-8") + "=" + URLEncoder.encode("10", "UTF-8")); urlBuilder.append("&" + URLEncoder.encode("pageNo", "UTF-8") + "=" + URLEncoder.encode("2", "UTF-8")); // ํ์ ์ ๋ ฅ ์กฐ๊ฑด ์ถ๊ฐ // json ํ์์ผ๋ก ๋ฐ์์ด urlBuilder.append("&" + URLEncoder.encode("resultType", "UTF-8") + "=" + URLEncoder.encode("json", "UTF-8")); URL url = new URL(urlBuilder.toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Content-type", "application/json"); // System.out.println("Response code: " + conn.getResponseCode()); BufferedReader rd; if (conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) { rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); } else { rd = new BufferedReader(new InputStreamReader(conn.getErrorStream())); } StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); // System.out.println(sb.toString()); return new JSONObject(sb.toString()); } }
RecruitService
public void saveRecruit() { RecruitAPI recruitAPI = new RecruitAPI(); // String์ผ๋ก ๋ค์ด์ค๋ ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ํ์ฑ DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd"); try { JSONObject jsonResponse = recruitAPI.getRecruitData(); // JSON ๊ฐ์ฒด ๋ฐ์์ค JSONArray items = jsonResponse.getJSONArray("result"); for (int i = 0; i < items.length(); i++) { JSONObject item = items.getJSONObject(i); Recruit recruit = new Recruit(); //ํ์ํ ์นผ๋ผ๋ง set recruit.setRecruitClass(item.getString("hireTypeNmLst")); recruit.setRecruitCategory(item.getString("ncsCdNmLst")); recruit.setCompany(item.getString("instNm")); recruit.setArea(item.getString("workRgnNmLst")); recruit.setCareer(item.getString("recrutSeNm")); // String์ผ๋ก ๋ค์ด์ค๋ ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ํ์ฑ String pbancBgngYmd = item.getString("pbancBgngYmd"); LocalDate startDate = LocalDate.parse(pbancBgngYmd, dateFormatter); LocalDateTime startDateTime = startDate.atStartOfDay(); recruit.setStartDate(startDateTime); String pbancEndYmd = item.getString("pbancEndYmd"); LocalDate endDate = LocalDate.parse(pbancEndYmd, dateFormatter); LocalDateTime endDateTime = endDate.atStartOfDay(); recruit.setEndDate(endDateTime); recruit.setSerialNumber(item.getString("pbadmsStdInstCd")); recruit.setUrl(item.getString("srcUrl")); recruitRepository.save(recruit); } } catch (Exception e) { e.printStackTrace(); } }
RecruitController
@PostMapping("/parse") @Operation(summary = "๊ณต๊ณ API ๋ฐ์ดํฐ ๋ฑ๋ก") public ResponseEntity<Void> enroll() { recruitService.saveRecruit(); return ResponseEntity.status(HttpStatus.CREATED).build(); }
4. ๊ฒฐ๊ณผ ํ์ธ
5. ์ฐธ๊ณ ํ ์๋ฃ
๊ฐ์ฌํฉ๋๋ค..๋ฐ๋ด๐โ๏ธ
'๐ธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring, Web Socket, STOMP, MongoDB ํ๊ฒฝ์์ ์ฑํ ๊ตฌํ (2) 2024.09.26 Kafka, Redis (0) 2024.09.17 [Java] ์์ฃผ ์ฌ์ฉ๋๋ Lombok ์ด๋ ธํ ์ด์ (0) 2024.06.22 [Python] deque ์๋ฃ๊ตฌ์กฐ (0) 2024.06.08 [Python] DFS (0) 2024.06.07