230421 how to use the open api with json.

William Parker·2023년 4월 21일

Open api to get and apply public holiday data.

The problem is that if there are two overlapping days on holidays, they are received in the form of a list, but when there is one, an error occurs because they are not received as a list.

There is a lot of duplicated data in that part, so I'm thinking about how to handle it. The code below works, but it's not as clean as I'd like.

@RestController
@RequestMapping("/api/user")
@JsonInclude(JsonInclude.Include.NON_NULL)
@RequiredArgsConstructor
public class HolidayOpenApi {

    @Value("${openApi.key.holiday}")
    private String serviceKey;

    @GetMapping("/national/holiday")
    public ResponseEntity nationalHolidayOpenApi(
        @RequestBody NationalHolidayRequest request)
        throws URISyntaxException, JsonProcessingException, MalformedURLException {

        final RestTemplate rest = new RestTemplate();
        URL uri = new URL(
            "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getRestDeInfo"
                + serviceKey //servicekey
                + "&solYear=" + request.getYear()  // year
                + "&solMonth=" + (request.getMonth() > 9 ? "" : "0") + request.getMonth() // month
        );

        final String response = rest.getForObject(uri.toURI(), String.class);
        if(response.contains("\"item\":[")){
        Resp resp = new ObjectMapper().readValue(response, Resp.class);
            return new ResponseEntity<>(
                resp.response.body.items.item.stream().map(NationalHolidayResponse::new).collect(
                    Collectors.toList()), HttpStatus.OK);
        } else {
            Resp1 resp = new ObjectMapper().readValue(response, Resp1.class);
            NationalHolidayResponse a = new NationalHolidayResponse(
                resp.response.body.items.item);
            return new ResponseEntity<>(a, HttpStatus.OK);

        }
    }

    @Data
    @ToString
    public static class Resp {

        private ApiDto response;
    }
    @Data
    @ToString
    public static class Resp1 {

        private ApiDto1 response;
    }
    @Data
    @ToString
    public static class ApiDto {

        private Map<String, String> header;
        private DtoItems body;
    }

    @Data
    @ToString
    public static class ApiDto1 {

        private Map<String, String> header;
        private DtoItems1 body;
    }

    @Data
    @ToString
    public static class DtoItems {

        private DtoItem items;
        private int numOfRows;
        private int pageNo;
        private int totalCount;
    }

    @Data
    @ToString
    public static class DtoItems1 {

        private DtoItem1 items;
        private int numOfRows;
        private int pageNo;
        private int totalCount;
    }

    @Data
    @ToString
    public static class DtoItem {

        private List<Dto> item;
    }

    @Data
    @ToString
    public static class DtoItem1 {

        private Dto item;
    }

    @Data
    @ToString
    public static class Dto {

        private String dateKind;
        private String dateName;
        private String isHoliday;
        private int locdate;
        private int seq;
    }
}
profile
Developer who does not give up and keeps on going.

0개의 댓글