Created
August 9, 2022 07:58
-
-
Save stella6767/83751e4de390808a69c2688da601a1ae to your computer and use it in GitHub Desktop.
공통 response body modify
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
@org.springframework.web.bind.annotation.RestControllerAdvice | |
class RestControllerAdvice<T>( | |
) : ResponseBodyAdvice<T>{ | |
private val log = KotlinLogging.logger { } | |
override fun supports(returnType: MethodParameter, converterType: Class<out HttpMessageConverter<*>>): Boolean { | |
//log.info { "returnType: $returnType" } | |
return true | |
} | |
override fun beforeBodyWrite( | |
body: T?, | |
returnType: MethodParameter, | |
selectedContentType: MediaType, | |
selectedConverterType: Class<out HttpMessageConverter<*>>, | |
request: ServerHttpRequest, | |
response: ServerHttpResponse | |
): T? { | |
return if (body is CMResDto<*>){ | |
//type casting 생략가눙 | |
val apiVersion = request.uri.path.split("/").first { it.isNotEmpty() } | |
log.info { """ | |
body: $body | |
request: ${request.uri.path} | |
api version: $apiVersion | |
""".trimIndent() } | |
body.reflectVersion(apiVersion) | |
body | |
}else{ | |
body | |
} | |
} | |
} | |
data class CMResDto<T>( | |
var resultCode: T, | |
var resultMsg: String, | |
var data: T | |
){ | |
fun reflectVersion(version: String){ | |
this.resultMsg = "version:$version / ${this.resultMsg}" | |
} | |
enum class ResultCode{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment