From 72033272c6580ffee7f60cae59093a21e911f382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=99=8D?= Date: Thu, 16 Mar 2023 09:50:22 +0900 Subject: [PATCH] =?UTF-8?q?:bug:=20Result=20=EA=B0=9D=EC=B2=B4=20=EB=A7=B5?= =?UTF-8?q?=ED=95=91=20=EC=95=88=EB=90=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../datakr/DataKrAutoConfiguration.kt | 31 +++++-------------- .../autoconfigure/datakr/DataKrProperties.kt | 1 + .../kotlin/herbaccara/datakr/DataKrService.kt | 30 +++++++++--------- .../herbaccara/datakr/model/HolidayResult.kt | 3 ++ .../herbaccara/datakr/model/HopitalResult.kt | 3 ++ 5 files changed, 28 insertions(+), 40 deletions(-) create mode 100644 src/main/kotlin/herbaccara/datakr/model/HolidayResult.kt create mode 100644 src/main/kotlin/herbaccara/datakr/model/HopitalResult.kt diff --git a/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrAutoConfiguration.kt b/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrAutoConfiguration.kt index cd2eb66..2ce586b 100644 --- a/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrAutoConfiguration.kt +++ b/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrAutoConfiguration.kt @@ -1,11 +1,9 @@ package herbaccara.boot.autoconfigure.datakr -import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.dataformat.xml.XmlMapper -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import herbaccara.datakr.DataKrService import org.springframework.boot.autoconfigure.AutoConfiguration -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.web.client.RestTemplateBuilder @@ -16,32 +14,17 @@ import java.util.* @AutoConfiguration @EnableConfigurationProperties(DataKrProperties::class) -@ConditionalOnProperty(prefix = "datakr", value = ["enabled"], havingValue = "true") +@ConditionalOnProperty(prefix = "datakr", value = ["enabled"], havingValue = "true", matchIfMissing = true) class DataKrAutoConfiguration { @Bean - @ConditionalOnMissingBean - fun objectMapper(): ObjectMapper { - return jacksonObjectMapper().apply { - findAndRegisterModules() - } - } + fun dataKrService(properties: DataKrProperties): DataKrService { + if (properties.serviceKey.isEmpty()) throw NullPointerException() - @Bean - @ConditionalOnMissingBean - fun xmlMapper(): XmlMapper { - return XmlMapper().apply { + val xmlMapper = XmlMapper().apply { findAndRegisterModules() + configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, properties.failOnUnknownProperties) } - } - - @Bean - fun dataKrService( - objectMapper: ObjectMapper, - xmlMapper: XmlMapper, - properties: DataKrProperties - ): DataKrService { - if (properties.serviceKey.isEmpty()) throw NullPointerException() val restTemplate = RestTemplateBuilder() .rootUri(properties.rootUri) @@ -50,6 +33,6 @@ class DataKrAutoConfiguration { ) .build() - return DataKrService(restTemplate, xmlMapper, objectMapper, properties) + return DataKrService(restTemplate, xmlMapper, properties) } } diff --git a/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrProperties.kt b/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrProperties.kt index e2111ef..6484d32 100644 --- a/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrProperties.kt +++ b/src/main/kotlin/herbaccara/boot/autoconfigure/datakr/DataKrProperties.kt @@ -8,5 +8,6 @@ import org.springframework.boot.context.properties.ConstructorBinding data class DataKrProperties( val enabled: Boolean = true, val rootUri: String = "https://apis.data.go.kr", + val failOnUnknownProperties: Boolean = false, val serviceKey: String ) diff --git a/src/main/kotlin/herbaccara/datakr/DataKrService.kt b/src/main/kotlin/herbaccara/datakr/DataKrService.kt index bd2b60d..9eedf31 100644 --- a/src/main/kotlin/herbaccara/datakr/DataKrService.kt +++ b/src/main/kotlin/herbaccara/datakr/DataKrService.kt @@ -1,12 +1,9 @@ package herbaccara.datakr -import com.fasterxml.jackson.core.type.TypeReference -import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.dataformat.xml.XmlMapper import herbaccara.boot.autoconfigure.datakr.DataKrProperties -import herbaccara.datakr.model.Holiday -import herbaccara.datakr.model.Hopital -import herbaccara.datakr.model.Result +import herbaccara.datakr.model.HolidayResult +import herbaccara.datakr.model.HopitalResult import org.springframework.util.LinkedMultiValueMap import org.springframework.web.client.RestTemplate import org.springframework.web.client.getForObject @@ -17,10 +14,9 @@ import java.net.URLEncoder class DataKrService( private val restTemplate: RestTemplate, private val xmlMapper: XmlMapper, - private val objectMapper: ObjectMapper, private val properties: DataKrProperties ) { - private fun getForObject(uri: String, params: Map): Result { + private fun getForObject(uri: String, params: Map, clazz: Class): T { val endpoint = UriComponentsBuilder .fromHttpUrl("${properties.rootUri}$uri") .queryParams( @@ -34,14 +30,14 @@ class DataKrService( val body: String = restTemplate.getForObject(URI(endpoint)) - return xmlMapper.readValue(body, object : TypeReference>() {}) + return xmlMapper.readValue(body, clazz) } /*** * 국립중앙의료원_국립중앙의료원_전국 병·의원 찾기 서비스 - 병/의원 FullData 내려받기 * https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15000736 */ - fun getHsptlMdcncFullDown(pageNo: Int, numOfRows: Int): Result { + fun getHsptlMdcncFullDown(pageNo: Int, numOfRows: Int): HopitalResult { val uri = "/B552657/HsptlAsembySearchService/getHsptlMdcncFullDown" return getForObject( @@ -49,14 +45,15 @@ class DataKrService( mapOf( "pageNo" to pageNo, "numOfRows" to numOfRows - ) + ), + HopitalResult::class.java ) } /*** * @param numOfRows 1000 ~ 5000 개 정도가 적당하다. */ - fun getHsptlMdcncFullDown(numOfRows: Int, block: (Result) -> Unit) { + fun getHsptlMdcncFullDown(numOfRows: Int, block: (HopitalResult) -> Unit) { val totalCount = getHsptlMdcncFullDown(1, 1).body.totalCount var totalPage = totalCount / numOfRows @@ -68,8 +65,8 @@ class DataKrService( .forEach { pageNo: Int -> block(getHsptlMdcncFullDown(pageNo, numOfRows)) } } - fun getHsptlMdcncFullDown(numOfRows: Int): List> { - val list = mutableListOf>() + fun getHsptlMdcncFullDown(numOfRows: Int): List { + val list = mutableListOf() getHsptlMdcncFullDown(numOfRows, list::add) return list } @@ -78,7 +75,7 @@ class DataKrService( * 한국천문연구원_특일 정보 - 공휴일 정보 조회 * https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15012690 */ - fun getRestDeInfo(solYear: Int, solMonth: Int): Result { + fun getRestDeInfo(solYear: Int, solMonth: Int): HolidayResult { val uri = "/B090041/openapi/service/SpcdeInfoService/getRestDeInfo" return getForObject( @@ -86,11 +83,12 @@ class DataKrService( mapOf( "solYear" to solYear, "solMonth" to solMonth.toString().padStart(2, '0') - ) + ), + HolidayResult::class.java ) } - fun getRestDeInfo(solYear: Int): List> { + fun getRestDeInfo(solYear: Int): List { return (1..12) .map { getRestDeInfo(solYear, it) } } diff --git a/src/main/kotlin/herbaccara/datakr/model/HolidayResult.kt b/src/main/kotlin/herbaccara/datakr/model/HolidayResult.kt new file mode 100644 index 0000000..2ddef29 --- /dev/null +++ b/src/main/kotlin/herbaccara/datakr/model/HolidayResult.kt @@ -0,0 +1,3 @@ +package herbaccara.datakr.model + +class HolidayResult(header: Header, body: Body) : Result(header, body) diff --git a/src/main/kotlin/herbaccara/datakr/model/HopitalResult.kt b/src/main/kotlin/herbaccara/datakr/model/HopitalResult.kt new file mode 100644 index 0000000..bb5369d --- /dev/null +++ b/src/main/kotlin/herbaccara/datakr/model/HopitalResult.kt @@ -0,0 +1,3 @@ +package herbaccara.datakr.model + +class HopitalResult(header: Header, body: Body) : Result(header, body)