Skip to content

Instantly share code, notes, and snippets.

Avatar

Bruna Pereira brunapereira

View GitHub Profile
View build.gradle.kts
plugins {
kotlin("jvm") version "1.3.72"
id("com.github.johnrengelman.shadow") version "4.0.4"
id("org.unbroken-dome.test-sets") version "3.0.1"
application
}
group = "com.brunapereira"
version = "1.0-SNAPSHOT"
View VendasExceptionHandler.kt
@RestControllerAdvice
@Order(0)
class VendasExceptionHandler {
@ResponseStatus(BAD_REQUEST)
@ExceptionHandler(ProductNotFound::class)
fun onProductNotFound(e: ProductNotFound): ApiError {
log.error("error=product not found", e)
return ApiError(code = "PRODUCT_NOT_FOUND", message = e.message ?: "")
}
@brunapereira
brunapereira / ExceptionHandler.kt
Last active May 11, 2020
Example of a Spring ControllerAdvice
View ExceptionHandler.kt
@RestControllerAdvice
class ExceptionHandler {
@ResponseStatus(INTERNAL_SERVER_ERROR) [1]
@ExceptionHandler(Exception::class)
fun onException(exception: Exception): ApiError {
log.error("error=general message=${exception.message}", exception)
return ApiError("INTERNAL_SERVER_ERROR", exception.message ?: "")
}
View WebClientBuilder.kt
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
}
View examples.kt
get(
url = "/login",
responseClass = Token::class,
queryParameters = mapOf("admin", "true")
)
get(
url = "/login",
responseClass = Token::class,
headers = mapOf("Accept-Version", "v1")
View MicroserviceGateway.kt
abstract class MicroserviceGateway {
abstract var webClient: WebClient
// outros métodos HTTP
fun <ResponseT : Any> get(
url: String,
responseClass: KClass<ResponseT>,
queryParameters: Map<String, String> = emptyMap(),
headers: Map<String, String> = emptyMap()
View AuthenticationProperties.kt
@Component
@ConfigurationProperties("service.authentication")
class AuthenticationProperties {
final lateinit var baseUrl: String
final lateinit var username: String
final lateinit var password: String
}
View AuthenticationGateway.kt
@Component
class AuthenticationGateway(private val properties: AuthenticationProperties [1]) : MicroserviceGateway() [2] {
override var webClient: WebClient = ClientBuilder() [3]
.withBaseUrl(properties.baseUrl)
.withFailOnUnknownProperty(false)
.withNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.build()
fun authenticate(): AuthenticationToken [4] {
val request =
View SwaggerResourceConfig.kt
@Configuration
class SwaggerResourceConfig {
@Primary
@Bean
fun swaggerResourcesProvider(defaultResourcesProvider: InMemorySwaggerResourcesProvider): SwaggerResourcesProvider {
return SwaggerResourcesProvider {
val resource = SwaggerResource()
resource.name = "Open API"
resource.swaggerVersion = "2.9.2"
View FF4JExtensions.kt
fun FF4j.isEnabled(featureName: String): Boolean = this.check(featureName)
fun FF4j.isDisabled(featureName: String): Boolean = !this.check(featureName)
You can’t perform that action at this time.