Skip to content

Commit

Permalink
🐛 Result 객체 맵핑 안되는 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
이용홍 committed Mar 16, 2023
1 parent fb76721 commit 7203327
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -50,6 +33,6 @@ class DataKrAutoConfiguration {
)
.build()

return DataKrService(restTemplate, xmlMapper, objectMapper, properties)
return DataKrService(restTemplate, xmlMapper, properties)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
30 changes: 14 additions & 16 deletions src/main/kotlin/herbaccara/datakr/DataKrService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <T> getForObject(uri: String, params: Map<String, Any>): Result<T> {
private fun <T> getForObject(uri: String, params: Map<String, Any>, clazz: Class<T>): T {
val endpoint = UriComponentsBuilder
.fromHttpUrl("${properties.rootUri}$uri")
.queryParams(
Expand All @@ -34,29 +30,30 @@ class DataKrService(

val body: String = restTemplate.getForObject(URI(endpoint))

return xmlMapper.readValue(body, object : TypeReference<Result<T>>() {})
return xmlMapper.readValue(body, clazz)
}

/***
* 국립중앙의료원_국립중앙의료원_전국 병·의원 찾기 서비스 - 병/의원 FullData 내려받기
* https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15000736
*/
fun getHsptlMdcncFullDown(pageNo: Int, numOfRows: Int): Result<Hopital> {
fun getHsptlMdcncFullDown(pageNo: Int, numOfRows: Int): HopitalResult {
val uri = "/B552657/HsptlAsembySearchService/getHsptlMdcncFullDown"

return getForObject(
uri,
mapOf(
"pageNo" to pageNo,
"numOfRows" to numOfRows
)
),
HopitalResult::class.java
)
}

/***
* @param numOfRows 1000 ~ 5000 개 정도가 적당하다.
*/
fun getHsptlMdcncFullDown(numOfRows: Int, block: (Result<Hopital>) -> Unit) {
fun getHsptlMdcncFullDown(numOfRows: Int, block: (HopitalResult) -> Unit) {
val totalCount = getHsptlMdcncFullDown(1, 1).body.totalCount

var totalPage = totalCount / numOfRows
Expand All @@ -68,8 +65,8 @@ class DataKrService(
.forEach { pageNo: Int -> block(getHsptlMdcncFullDown(pageNo, numOfRows)) }
}

fun getHsptlMdcncFullDown(numOfRows: Int): List<Result<Hopital>> {
val list = mutableListOf<Result<Hopital>>()
fun getHsptlMdcncFullDown(numOfRows: Int): List<HopitalResult> {
val list = mutableListOf<HopitalResult>()
getHsptlMdcncFullDown(numOfRows, list::add)
return list
}
Expand All @@ -78,19 +75,20 @@ class DataKrService(
* 한국천문연구원_특일 정보 - 공휴일 정보 조회
* https://www.data.go.kr/tcs/dss/selectApiDataDetailView.do?publicDataPk=15012690
*/
fun getRestDeInfo(solYear: Int, solMonth: Int): Result<Holiday> {
fun getRestDeInfo(solYear: Int, solMonth: Int): HolidayResult {
val uri = "/B090041/openapi/service/SpcdeInfoService/getRestDeInfo"

return getForObject(
uri,
mapOf(
"solYear" to solYear,
"solMonth" to solMonth.toString().padStart(2, '0')
)
),
HolidayResult::class.java
)
}

fun getRestDeInfo(solYear: Int): List<Result<Holiday>> {
fun getRestDeInfo(solYear: Int): List<HolidayResult> {
return (1..12)
.map { getRestDeInfo(solYear, it) }
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/herbaccara/datakr/model/HolidayResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package herbaccara.datakr.model

class HolidayResult(header: Header, body: Body<Holiday>) : Result<Holiday>(header, body)
3 changes: 3 additions & 0 deletions src/main/kotlin/herbaccara/datakr/model/HopitalResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package herbaccara.datakr.model

class HopitalResult(header: Header, body: Body<Hopital>) : Result<Hopital>(header, body)

0 comments on commit 7203327

Please sign in to comment.