Last active
January 11, 2020 22:46
-
-
Save brunapereira/30068cd699f5054e4bb84a3dee80848f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ClientBuilder { | |
private var failOnUnknownProperty: Boolean = false | |
private var namingStrategy: PropertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE | |
private var headers: MutableMap<String, String> = mutableMapOf() | |
private lateinit var baseUrl: String | |
fun withFailOnUnknownProperty(failOnUnknown: Boolean): ClientBuilder { | |
this.failOnUnknownProperty = failOnUnknown | |
return this | |
} | |
fun withNamingStrategy(namingStrategy: PropertyNamingStrategy): ClientBuilder { | |
this.namingStrategy = namingStrategy | |
return this | |
} | |
fun withBaseUrl(baseUrl: String): ClientBuilder { | |
this.baseUrl = baseUrl | |
return this | |
} | |
fun withHeader(pair: Pair<String, String>) { | |
this.headers[pair.first] = pair.second | |
} | |
fun build(): WebClient { | |
val objectMapper = ObjectMapper().registerKotlinModule() | |
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | |
objectMapper.propertyNamingStrategy = namingStrategy | |
val exchangeStrategies: ExchangeStrategies = ExchangeStrategies.builder() | |
.codecs { configurer -> | |
configurer.defaultCodecs() | |
.jackson2JsonEncoder(Jackson2JsonEncoder(objectMapper, MediaType.APPLICATION_JSON)) | |
configurer.defaultCodecs() | |
.jackson2JsonDecoder(Jackson2JsonDecoder(objectMapper, MediaType.APPLICATION_JSON)) | |
} | |
.build() | |
return WebClient.builder() | |
.baseUrl(baseUrl) | |
.exchangeStrategies(exchangeStrategies) | |
.build() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment