서론

package com.example.demo.domain.transitstop.service

import com.example.demo.common.feign.client.QueryTransitStopClient
import com.example.demo.common.properties.ApiKeyProperties
import com.example.demo.domain.transitstop.presentation.dto.response.TransitStopList.TransitStopElement
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.springframework.stereotype.Service
import java.net.URLEncoder

@Service
class TransitStopService(
    private val queryTransitStopClient: QueryTransitStopClient,
    private val apiKeyProperties: ApiKeyProperties,
) {

    companion object {
        const val STATION_NAME = "stationName"
        const val STATION_ID = "stationID"
        const val ARS_ID = "arsID"
        const val BUS_NUMBER = "busNo"
    }

    fun getTransitStop(stationName: String): List<TransitStopElement> {
        val apiKey = URLEncoder.encode(apiKeyProperties.key, "UTF-8")
        val transitStop = queryTransitStopClient.getTransitStop(
            apiKey = apiKey,
            stationName = stationName
        )
        return dataParsing(transitStop)
    }

    private fun dataParsing(transitStopInfo: String): List<TransitStopElement> =
        jacksonObjectMapper().readValue<JsonNode>(transitStopInfo)
            //.findValue("result")
            //.findValue("station")
            .map { jsonValues ->
                TransitStopElement(
                    stationName = jsonValues.findValueByFieldName(STATION_NAME),
                    stationId = jsonValues.findValueByFieldName(STATION_ID).toInt(),
                    arsId = jsonValues.findValueByFieldName(ARS_ID),
                    busNumber = jsonValues.findValueByFieldName(BUS_NUMBER),
                )
            }

    private fun JsonNode.findValueByFieldName(fieldName: String): String {
        try {
            return this.findValue(fieldName).asText()
        } catch (e: NullPointerException) {
            throw IllegalArgumentException("추후 404 처리")
        }
    }
}

json 파싱을 하던 도중 발생한 것인데 저 위의 findValue 2개를 사용하지 않아도 알아서 가져와질거라고

생각해서 주석 처리하고 실행을 해봤더니 **결과가 가장 첫 번째 하나밖에 조회**되지 않았다.

그래서 findValue 저거 2개를 적어주니 리스트로 잘 나오는 걸 확인할 수 있었다.


파싱

{"result":{"CID":[{"cityRegion":"대전","cityName":"대전","cityCode":"3000"}]}}
[CityCodeElement(cityRegion=대전, cityName=대전, cityCode=3000)]

{"result":{"totalCityList":{"includeCity":[{"cityName":"대전","CID":3000}]},"totalCount":2,"station":[{"stationClass":1,"stationName":"은어송마을5단지","stationID":366047,"x":127.458809,"y":36.308487,"CID":3000,"cityName":"대전","arsID":"10-490","do":"대전광역시","gu":"동구","dong":"효동","businfo":[{"busClass":"11","busLocalBlID":"DJB30300130","busNo":"608"},{"busClass":"11","busLocalBlID":"DJB30300082","busNo":"618"},{"busClass":"11","busLocalBlID":"DJB30300067","busNo":"514"}],"localStationID":"DJB8002447","ebid":"10-490","stationDirectionName":"동구청"},{"stationClass":1,"stationName":"은어송마을5단지","stationID":366048,"x":127.459376,"y":36.308616,"CID":3000,"cityName":"대전","arsID":"10-500","do":"대전광역시","gu":"동구","dong":"효동","businfo":[{"busClass":"11","busLocalBlID":"DJB30300130","busNo":"608"},{"busClass":"11","busLocalBlID":"DJB30300082","busNo":"618"},{"busClass":"11","busLocalBlID":"DJB30300067","busNo":"514"}],"localStationID":"DJB8002448","ebid":"10-500","stationDirectionName":"은어송마을4단지"}]}}
StationList(stationList=[StationElement(stationName=은어송마을5단지, stationId=366047, arsId=10-490, busNumber=608), StationElement(stationName=은어송마을5단지, stationId=366048, arsId=10-500, busNumber=608)])