공공데이터 오픈 API 사용하기(휴일 정보 - 한국천문연구원_특일 정보)

2022. 6. 13. 01:42·WEB/Spring

 

공휴일 정보를 불러오기 위해 공공데이터인 한국천문연구원_특일 정보 open API를 사용했다.

 

 

https://www.data.go.kr/index.do

 

공공데이터 포털

국가에서 보유하고 있는 다양한 데이터를『공공데이터의 제공 및 이용 활성화에 관한 법률(제11956호)』에 따라 개방하여 국민들이 보다 쉽고 용이하게 공유•활용할 수 있도록 공공데이터(Datase

www.data.go.kr

활용신청을 클릭하고 사유를 입력하면 바로 사용이 가능하다!! ( 로그인 필요 )

 

▼

위 이미지에서 사용할 API를 클릭하면 상세 정보를 확인할 수 있다.

 

 

해당 페이지에서 url에 필요한 인증키를 확인할 수 있다.

 

 

아래의 API 상세 페이지에서 api를 호출하는 sample 코드를 제공하고 있어서 이를 활용했다.

 

한국천문연구원_특일 정보

(천문우주정보)국경일정보, 공휴일정보, 기념일정보, 24절기정보, 잡절정보를 조회하는 서비스 입니다. 활용시 날짜, 순번, 특일정보의 분류, 공공기관 휴일 여부, 명칭을 확인할 수 있습니다.

www.data.go.kr

 

 

공휴일 정보를 조회하는 JAVA sample 코드를 사용한 Spring Boot Controller이다.

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.io.BufferedReader;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

    @ResponseBody 
    @RequestMapping("/openAPItest_v1")
    public String openAPItest_v1(String organization, String yearMonth) throws IOException{
    	StringBuilder urlBuilder = new StringBuilder("http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo"); /*URL*/
        urlBuilder.append("?" + URLEncoder.encode("serviceKey","UTF-8") + "="+ "인증키 입력할 부분"); /*Service Key*/
        urlBuilder.append("&" + URLEncoder.encode("solYear","UTF-8") + "=" + URLEncoder.encode("2019", "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) { //http status code check
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        } else {
            rd = new BufferedReader(new InputStreamReader(conn.getErrorStream(), "UTF-8"));
        }
    	
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        rd.close();
        conn.disconnect();
        System.out.println(sb.toString());

        return sb.toString();
    }

 

결과

 

데이터는 잘 받아오지만 보기 좋지 않았다. 

 

그래서 사이트에 있는 해당 open api의 가이드를 찾아보았다.

 

문서를 찾아보니 사이트에 없는 파라미터가 2개가 있었다.

xml보다 json으로 처리하는 게 값을 확인하기 편리할 거 같아서 _type 파라미터의 값을 json으로 하고

해당 년도의 모든 공휴일을 확인하고 싶어서 numOfRows 파라미터의 값을 365로 줬다.

 

 

***수정 내역***

StringBuilder urlBuilder = new StringBuilder("http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo"); /*URL*/
        urlBuilder.append("?" + URLEncoder.encode("serviceKey","UTF-8") + "=q6mx0UzQv%2FGBiGy4vIDna4fOCB7RPKvyBtiKDrtt5v0LKE5EKwzsdAH5BdM7mfmLd49dVyH7siHcjUhFa1e7Eg%3D%3D"); /*Service Key*/
        urlBuilder.append("&" + URLEncoder.encode("_type","UTF-8") + "=" + URLEncoder.encode("json", "UTF-8")); /*타입*/
        urlBuilder.append("&" + URLEncoder.encode("solYear","UTF-8") + "=" + URLEncoder.encode("2019", "UTF-8")); /*연*/
        urlBuilder.append("&" + URLEncoder.encode("numOfRows","UTF-8") + "=" + URLEncoder.encode("365", "UTF-8")); /*최대로 출력할 공휴일 수*/

 

 

결과

해당 년도의 전체 공휴일을 확인할 수 있고 한눈에 값을 확인할 수 있다!!

'WEB > Spring' 카테고리의 다른 글

Open API에서 공휴일 날짜 파싱 (JSON)  (0) 2022.06.13
Spring: log4j.xml 에러  (0) 2022.03.27
Spring: root-context.xml 에러  (0) 2022.03.27
Spring : Tomcat 설정  (0) 2022.03.24
Spring : Eclipse 설치하고 spring 개발 환경 세팅하기 for MAC  (0) 2022.03.24
'WEB/Spring' 카테고리의 다른 글
  • Open API에서 공휴일 날짜 파싱 (JSON)
  • Spring: log4j.xml 에러
  • Spring: root-context.xml 에러
  • Spring : Tomcat 설정
HBean_
HBean_
백엔드 개발자의 개발 로그 💻
  • HBean_
    개발_log
    HBean_
  • 전체
    오늘
    어제
    • 전체 (102)
      • WEB (49)
        • Spring (14)
        • AWS EC2 (6)
        • DB (3)
        • 2020_webCamp (25)
        • JPA (1)
      • Devops (2)
      • 보안 (4)
      • Git (6)
      • JAVA (13)
      • 자료구조 (2)
      • 알고리즘 (10)
      • 네트워크 (2)
      • SStudy (2)
      • 실전프로젝트2 (4)
      • 개발 일기 (1)
      • 개발툴 (4)
      • Intellij (2)
      • 이슈 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • GITHUB
  • 공지사항

  • 인기 글

  • 태그

    플러그인
    웹
    톰캣
    인텔리제이
    IntelliJ
    tomcat
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
HBean_
공공데이터 오픈 API 사용하기(휴일 정보 - 한국천문연구원_특일 정보)
상단으로

티스토리툴바