Skip to content

Commit

Permalink
Merge pull request #16 from HongDam-org/feat/bean-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 authored Sep 16, 2023
2 parents 689259d + 8cc7dcb commit f9f0cce
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 110 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,18 +15,21 @@
import java.nio.charset.StandardCharsets;

@Component
public class SearchPathClient implements PathClient<SearchPathRequest, SearchPathResponse> {
public class SearchPathClient implements MapClient<SearchPathRequest, SearchPathResponse> {
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;
}

/*상세 검색을 위한 변경 필요*/
private URI getPathUri(final SearchPathRequest request, final UriBuilder uriBuilder) {

final UriBuilder builder =
uriBuilder
.path(naverProperties.getUrl())
.path("driving")
.queryParam("start", request.getStart())
.queryParam("goal", request.getEnd())
Expand All @@ -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();
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SearchPathRequest, SearchPathResponse> client;
private final MapClient<SearchPathRequest, SearchPathResponse> client;

public PathService(PathClient<SearchPathRequest, SearchPathResponse> client) {
public PathService(MapClient<SearchPathRequest, SearchPathResponse> client) {
this.client = client;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,22 +19,23 @@
import java.nio.charset.StandardCharsets;

@Component
@RequiredArgsConstructor
public class SearchDestinationClient
implements MapClient<SearchDestinationRequest, SearchDestinationResponse> {
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) {
return webClient
.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)
Expand All @@ -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())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f9f0cce

Please sign in to comment.