IT/development

[springBoot] 공휴일 조회 API

알 수 없는 사용자 2023. 5. 15. 21:10
반응형

Open API 이용

service

package kr.test.dev.Service.api;


import kr.test.dev.Service.annual.AnnualService;
import kr.test.dev.Vo.HolidayItemVo;
import kr.test.dev.Vo.HolidayResponseVo;
import kr.test.dev.Vo.HolidayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
//공공데이터 특일정보 API 조회
public class ApiServiceImpl implements ApiService{

    @Autowired AnnualService annualService;

    private final String holidayURL = "https://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getHoliDeInfo";

    @Value("${openApi.serviceKey}")
    //오픈 API 키는 application.yml에서 불러옴
    private String serviceKey;

    /**
     * 특일정보 api 조회
     * @param year
     * @return
     */
    @Override
    public List<HolidayItemVo> getholidayInfo(String year) {

        RestTemplate restTemplate;
        // api 정보 결과값 저장용 list
        List<HolidayItemVo> items = new ArrayList<>();
        // 공휴일 정보 db 저장용 vo
        HolidayVo holidayVo;
        
        // 1~12월 공휴일 정보 조회
        for (int i = 1; i < 13; i++) {

            String solMonth = (i < 10) ? "0" + i : String.valueOf(i);

                try {
                URI requestURI = new URI(holidayURL + "?serviceKey=" + serviceKey + "&solYear=" + year + "&solMonth=" + solMonth);
//                log.info("특일정보 requestURI = [{}]", requestURI);
				// requestURI 형식 -> https://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getHoliDeInfo?serviceKey=발급받은서비스키&solYear=2023(년도)&solMonth=01(월)
                restTemplate = new RestTemplate();
                //api 결과값
                HolidayResponseVo response = restTemplate.getForObject(requestURI, HolidayResponseVo.class);
                
                items = response.getBody().getItems();
                    // 공휴일 정보 db 저장
                    for (HolidayItemVo item : items) {
                        if ("Y".equals(item.getIsHoliday())) {
                            holidayVo = new HolidayVo();
                            holidayVo.setHolidayDate(item.getLocdate());
                            holidayVo.setHolidayNm(item.getDateName());
//                            annualService.insertHolidayInfo(holidayVo);
                        }
                    }
                for (HolidayItemVo item : items) log.info("특일 정보 = [{}]", item.toString());
            } catch (Exception e) {
                log.info("Exception :: {}", e.getMessage());
            }
        }
        return items;
    }
}

controller

    /**
     * 특일 정보 조회
     * @return
     */
    @GetMapping(value = "holiday")
    @ResponseBody
    public List<HolidayItemVo> holidayList(@RequestParam String year) {
        // 해당하는 년도의 특일정보를 조회 후 db에 저장
        return apiService.getholidayInfo(year);
    }

HolidayItemVo

package kr.test.dev.Vo;

import lombok.Data;

import javax.xml.bind.annotation.XmlRootElement;

@Data
@XmlRootElement(name = "item")
//특일 item vo
public class HolidayItemVo {

    private String locdate;
    private String dateKind;
    private String isHoliday;
    private String dateName;
    private int seq;
}

HolidayItemsVo(list)

package kr.test.dev.Vo;

import lombok.Data;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@Data
@XmlRootElement(name = "body")
public class HolidayItemsVo {

    private List<HolidayItemVo> items;

    @XmlElementWrapper(name = "items")
    @XmlElement(name = "item")
    public List<HolidayItemVo> getItems() {
        return items;
    }
}

 

HolidayResponseVo

package kr.test.dev.Vo;

import lombok.Data;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.Map;

@Data
@XmlRootElement(name = "response")
public class HolidayResponseVo {

    private Map<String, Object> header;
    private HolidayItemsVo body;
}

HolidayVo(db 저장용)

package kr.test.dev.Vo;

import kr.renosoft.portal.common.CommonVo;
import lombok.Data;

@Data
public class HolidayVo extends CommonVo {

    private int holidaySno;             // 공휴일 순번
    private String holidayDate;        // 공휴일 날짜
    private String holidayNm;           // 공휴일 명칭
}

결과값(xml을 java로 변환)

결과값(xml)

참조: 어떤 블로그분 이었는데 기억이 안난다. 감사합니다.

반응형