I really like Spring's ResponseBodyAdvice, it's a feature I only recently discovered. Here is a simple one that I wrote:
@ControllerAdvice
class ClientResponseAdvice : ResponseBodyAdvice<Client> {
override fun supports(method: MethodParameter, clazz: Class<out HttpMessageConverter<*>>): Boolean {
return method.parameterType == Client::class.java
}
override fun beforeBodyWrite(client: Client?, param: MethodParameter, mediaType: MediaType, clazz: Class<out HttpMessageConverter<*>>, req: ServerHttpRequest, res: ServerHttpResponse): Client? {
return client?.copy(clientSecret = "")
}
}
In this case, I'm using the advice to remove the clientSecret from the response payload, so no response can be sent out with this sensitive value. The problem is that in my controllers, sometimes I want to return a ResponseEntity<Client> instead of a Client directly (different status codes/headers/etc). When I do this, the ResponseBodyAdvice fails to properly detect the Client class, and this doesn't execute.
I've been exploring every idea I can come up with on my own to fix this and get it working again. I've so far hit a dead end.