diff --git a/backend/src/main/java/com/twtw/backend/config/client/NaverWebClientConfig.java b/backend/src/main/java/com/twtw/backend/config/client/NaverWebClientConfig.java deleted file mode 100644 index df432a8f..00000000 --- a/backend/src/main/java/com/twtw/backend/config/client/NaverWebClientConfig.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.twtw.backend.config.client; - -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.codec.json.Jackson2JsonDecoder; -import org.springframework.web.reactive.function.client.ExchangeStrategies; -import org.springframework.web.reactive.function.client.WebClient; - -@Configuration -public class NaverWebClientConfig { - private static final String HEADER_CLIENT_ID = "X-NCP-APIGW-API-KEY-ID"; - private static final String HEADER_CLIENT_SECRET = "X-NCP-APIGW-API-KEY"; - private final ObjectMapper objectMapper; - - @Autowired - public NaverWebClientConfig(@Qualifier("naverObjectMapper") ObjectMapper objectMapper) { - this.objectMapper = objectMapper; - } - - @Bean(name = "NaverWebClient") - public WebClient webClient( - @Value("${naver-map.url}") final String url, - @Value("${naver-map.id}") final String clientId, - @Value("${naver-map.secret}") final String secretKey) { - - final ExchangeStrategies exchangeStrategies = - ExchangeStrategies.builder() - .codecs( - configurer -> { - configurer.defaultCodecs().maxInMemorySize(-1); - configurer - .defaultCodecs() - .jackson2JsonDecoder( - new Jackson2JsonDecoder(objectMapper)); - }) - .build(); - - return WebClient.builder() - .exchangeStrategies(exchangeStrategies) - .baseUrl(url) - .defaultHeader(HEADER_CLIENT_ID, clientId) - .defaultHeader(HEADER_CLIENT_SECRET, secretKey) - .build(); - } -} diff --git a/backend/src/main/java/com/twtw/backend/config/client/KakaoWebClientConfig.java b/backend/src/main/java/com/twtw/backend/config/client/WebClientConfig.java similarity index 56% rename from backend/src/main/java/com/twtw/backend/config/client/KakaoWebClientConfig.java rename to backend/src/main/java/com/twtw/backend/config/client/WebClientConfig.java index 193eec9f..542f4e4c 100644 --- a/backend/src/main/java/com/twtw/backend/config/client/KakaoWebClientConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/client/WebClientConfig.java @@ -2,28 +2,21 @@ import com.fasterxml.jackson.databind.ObjectMapper; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; +import lombok.RequiredArgsConstructor; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpHeaders; import org.springframework.http.codec.json.Jackson2JsonDecoder; import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.WebClient; @Configuration -public class KakaoWebClientConfig { - private static final String HEADER_PREFIX = "KakaoAK "; +@RequiredArgsConstructor +public class WebClientConfig { private final ObjectMapper objectMapper; - public KakaoWebClientConfig(@Qualifier("kakaoObjectMapper") ObjectMapper objectMapper) { - this.objectMapper = objectMapper; - } - - @Bean(name = "KakaoWebClient") - public WebClient webClient( - @Value("${kakao-map.url}") final String url, - @Value("${kakao-map.key}") final String authHeader) { + @Bean + public WebClient webClient() { final ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder() .codecs( @@ -36,10 +29,6 @@ public WebClient webClient( }) .build(); - return WebClient.builder() - .exchangeStrategies(exchangeStrategies) - .baseUrl(url) - .defaultHeader(HttpHeaders.AUTHORIZATION, HEADER_PREFIX + authHeader) - .build(); + return WebClient.builder().exchangeStrategies(exchangeStrategies).build(); } } diff --git a/backend/src/main/java/com/twtw/backend/config/mapper/CompositePropertyNamingStrategy.java b/backend/src/main/java/com/twtw/backend/config/mapper/CompositePropertyNamingStrategy.java new file mode 100644 index 00000000..06848ac5 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/config/mapper/CompositePropertyNamingStrategy.java @@ -0,0 +1,44 @@ +package com.twtw.backend.config.mapper; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.cfg.MapperConfig; +import com.fasterxml.jackson.databind.introspect.AnnotatedField; +import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; + +public class CompositePropertyNamingStrategy extends PropertyNamingStrategy { + private final PropertyNamingStrategy[] strategies; + + public CompositePropertyNamingStrategy(final PropertyNamingStrategy... strategies) { + this.strategies = strategies; + } + + @Override + public String nameForField( + final MapperConfig config, final AnnotatedField field, final String defaultName) { + String name = defaultName; + for (final PropertyNamingStrategy strategy : strategies) { + name = strategy.nameForField(config, field, name); + } + return name; + } + + @Override + public String nameForGetterMethod( + final MapperConfig config, final AnnotatedMethod method, final String defaultName) { + String name = defaultName; + for (final PropertyNamingStrategy strategy : strategies) { + name = strategy.nameForGetterMethod(config, method, name); + } + return name; + } + + @Override + public String nameForSetterMethod( + final MapperConfig config, final AnnotatedMethod method, final String defaultName) { + String name = defaultName; + for (final PropertyNamingStrategy strategy : strategies) { + name = strategy.nameForSetterMethod(config, method, name); + } + return name; + } +} diff --git a/backend/src/main/java/com/twtw/backend/config/mapper/NaverObjectMapperConfig.java b/backend/src/main/java/com/twtw/backend/config/mapper/NaverObjectMapperConfig.java deleted file mode 100644 index cae83b16..00000000 --- a/backend/src/main/java/com/twtw/backend/config/mapper/NaverObjectMapperConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.twtw.backend.config.mapper; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategies; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class NaverObjectMapperConfig { - - @Bean - public ObjectMapper naverObjectMapper() { - return new ObjectMapper() - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) - .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) - .setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE); - } -} diff --git a/backend/src/main/java/com/twtw/backend/config/mapper/KakaoObjectMapperConfig.java b/backend/src/main/java/com/twtw/backend/config/mapper/ObjectMapperConfig.java similarity index 66% rename from backend/src/main/java/com/twtw/backend/config/mapper/KakaoObjectMapperConfig.java rename to backend/src/main/java/com/twtw/backend/config/mapper/ObjectMapperConfig.java index da25cd73..9ee426cc 100644 --- a/backend/src/main/java/com/twtw/backend/config/mapper/KakaoObjectMapperConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/mapper/ObjectMapperConfig.java @@ -9,14 +9,17 @@ import org.springframework.context.annotation.Primary; @Configuration -public class KakaoObjectMapperConfig { +public class ObjectMapperConfig { - @Primary @Bean - public ObjectMapper kakaoObjectMapper() { + @Primary + public ObjectMapper objectMapper() { return new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true) - .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + .setPropertyNamingStrategy( + new CompositePropertyNamingStrategy( + PropertyNamingStrategies.SNAKE_CASE, + PropertyNamingStrategies.LOWER_CAMEL_CASE)); } } diff --git a/backend/src/main/java/com/twtw/backend/config/properties/PropertiesConfig.java b/backend/src/main/java/com/twtw/backend/config/properties/PropertiesConfig.java new file mode 100644 index 00000000..fd975e8d --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/config/properties/PropertiesConfig.java @@ -0,0 +1,11 @@ +package com.twtw.backend.config.properties; + +import com.twtw.backend.global.properties.KakaoProperties; +import com.twtw.backend.global.properties.NaverProperties; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties({NaverProperties.class, KakaoProperties.class}) +public class PropertiesConfig {} diff --git a/backend/src/main/java/com/twtw/backend/domain/path/client/SearchPathClient.java b/backend/src/main/java/com/twtw/backend/domain/path/client/SearchPathClient.java index e9f5c09b..3d158c48 100644 --- a/backend/src/main/java/com/twtw/backend/domain/path/client/SearchPathClient.java +++ b/backend/src/main/java/com/twtw/backend/domain/path/client/SearchPathClient.java @@ -2,10 +2,10 @@ import com.twtw.backend.domain.path.dto.client.SearchPathRequest; import com.twtw.backend.domain.path.dto.client.SearchPathResponse; -import com.twtw.backend.global.client.PathClient; +import com.twtw.backend.global.client.MapClient; import com.twtw.backend.global.exception.WebClientResponseException; +import com.twtw.backend.global.properties.NaverProperties; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; @@ -15,11 +15,13 @@ import java.nio.charset.StandardCharsets; @Component -public class SearchPathClient implements PathClient { +public class SearchPathClient implements MapClient { private final WebClient webClient; + private final NaverProperties naverProperties; - public SearchPathClient(@Qualifier("NaverWebClient") WebClient webClient) { + public SearchPathClient(final WebClient webClient, final NaverProperties naverProperties) { this.webClient = webClient; + this.naverProperties = naverProperties; } /*상세 검색을 위한 변경 필요*/ @@ -27,6 +29,7 @@ private URI getPathUri(final SearchPathRequest request, final UriBuilder uriBuil final UriBuilder builder = uriBuilder + .path(naverProperties.getUrl()) .path("driving") .queryParam("start", request.getStart()) .queryParam("goal", request.getEnd()) @@ -36,7 +39,7 @@ private URI getPathUri(final SearchPathRequest request, final UriBuilder uriBuil String wayPoints = request.getWay(); - if (wayPoints == "") { + if (wayPoints.isEmpty()) { return builder.build(); } @@ -50,6 +53,8 @@ public SearchPathResponse request(final SearchPathRequest request) { .uri(uri -> getPathUri(request, uri)) .accept(MediaType.APPLICATION_JSON) .acceptCharset(StandardCharsets.UTF_8) + .header(naverProperties.getHeaderClientId(), naverProperties.getId()) + .header(naverProperties.getHeaderClientSecret(), naverProperties.getSecret()) .retrieve() .bodyToMono(SearchPathResponse.class) .blockOptional() diff --git a/backend/src/main/java/com/twtw/backend/domain/path/service/PathService.java b/backend/src/main/java/com/twtw/backend/domain/path/service/PathService.java index a3a30397..879165f8 100644 --- a/backend/src/main/java/com/twtw/backend/domain/path/service/PathService.java +++ b/backend/src/main/java/com/twtw/backend/domain/path/service/PathService.java @@ -2,15 +2,15 @@ import com.twtw.backend.domain.path.dto.client.SearchPathRequest; import com.twtw.backend.domain.path.dto.client.SearchPathResponse; -import com.twtw.backend.global.client.PathClient; +import com.twtw.backend.global.client.MapClient; import org.springframework.stereotype.Service; @Service public class PathService { - private final PathClient client; + private final MapClient client; - public PathService(PathClient client) { + public PathService(MapClient client) { this.client = client; } diff --git a/backend/src/main/java/com/twtw/backend/domain/plan/client/SearchDestinationClient.java b/backend/src/main/java/com/twtw/backend/domain/plan/client/SearchDestinationClient.java index 4bb38d58..6285e796 100644 --- a/backend/src/main/java/com/twtw/backend/domain/plan/client/SearchDestinationClient.java +++ b/backend/src/main/java/com/twtw/backend/domain/plan/client/SearchDestinationClient.java @@ -5,8 +5,11 @@ import com.twtw.backend.domain.plan.entity.CategoryGroupCode; import com.twtw.backend.global.client.MapClient; import com.twtw.backend.global.exception.WebClientResponseException; +import com.twtw.backend.global.properties.KakaoProperties; -import org.springframework.beans.factory.annotation.Qualifier; +import lombok.RequiredArgsConstructor; + +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; @@ -16,15 +19,13 @@ import java.nio.charset.StandardCharsets; @Component +@RequiredArgsConstructor public class SearchDestinationClient implements MapClient { private static final Integer MAX_SIZE_PER_REQUEST = 15; private static final Integer DEFAULT_DISTANCE_RADIUS = 20000; private final WebClient webClient; - - public SearchDestinationClient(@Qualifier("KakaoWebClient") WebClient webClient) { - this.webClient = webClient; - } + private final KakaoProperties kakaoProperties; @Override public SearchDestinationResponse request(final SearchDestinationRequest request) { @@ -32,6 +33,9 @@ public SearchDestinationResponse request(final SearchDestinationRequest request) .get() .uri(uriBuilder -> getUri(request, uriBuilder)) .accept(MediaType.APPLICATION_JSON) + .header( + HttpHeaders.AUTHORIZATION, + kakaoProperties.getHeaderPrefix() + kakaoProperties.getKey()) .acceptCharset(StandardCharsets.UTF_8) .retrieve() .bodyToMono(SearchDestinationResponse.class) @@ -42,6 +46,7 @@ public SearchDestinationResponse request(final SearchDestinationRequest request) private URI getUri(final SearchDestinationRequest request, final UriBuilder uriBuilder) { final UriBuilder builder = uriBuilder + .path(kakaoProperties.getUrl()) .path("search/keyword") .queryParam("query", request.getQuery()) .queryParam("x", request.getX()) diff --git a/backend/src/main/java/com/twtw/backend/global/client/PathClient.java b/backend/src/main/java/com/twtw/backend/global/client/PathClient.java deleted file mode 100644 index 393e2ba6..00000000 --- a/backend/src/main/java/com/twtw/backend/global/client/PathClient.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.twtw.backend.global.client; - -public interface PathClient { - public R request(T request); -} diff --git a/backend/src/main/java/com/twtw/backend/global/properties/KakaoProperties.java b/backend/src/main/java/com/twtw/backend/global/properties/KakaoProperties.java new file mode 100644 index 00000000..2b088839 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/global/properties/KakaoProperties.java @@ -0,0 +1,15 @@ +package com.twtw.backend.global.properties; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@Getter +@RequiredArgsConstructor +@ConfigurationProperties(prefix = "kakao-map") +public class KakaoProperties { + private final String url; + private final String key; + private final String headerPrefix; +} diff --git a/backend/src/main/java/com/twtw/backend/global/properties/NaverProperties.java b/backend/src/main/java/com/twtw/backend/global/properties/NaverProperties.java new file mode 100644 index 00000000..53656f86 --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/global/properties/NaverProperties.java @@ -0,0 +1,17 @@ +package com.twtw.backend.global.properties; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@Getter +@RequiredArgsConstructor +@ConfigurationProperties(prefix = "naver-map") +public class NaverProperties { + private final String id; + private final String secret; + private final String url; + private final String headerClientId; + private final String headerClientSecret; +}