I hava a simple controller, it has two method, One is synchronous and the other is asynchronous
@RestController
class HelloController {
@PostMapping("/msg")
fun createMsg(@RequestBody maps: Map<String, String>): Map<String, String> {
println(maps)
return mapOf("code" to "success")
}
@PostMapping("/async/msg")
fun asyncCreateMsg(@RequestBody maps: Map<String, String>) = GlobalScope.future{
println(maps)
mapOf("code" to "success")
}
}
I write a filter to log request and response body
@Component
class ApplicationRequestFilter : OncePerRequestFilter() {
companion object {
private val logger = LoggerFactory.getLogger(this::class.java)
}
override fun doFilterInternal(request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain) {
val currTime = System.currentTimeMillis()
val requestWrapper = ContentCachingRequestWrapper(request)
val responseWrapper = ContentCachingResponseWrapper(response)
filterChain.doFilter(requestWrapper, responseWrapper)
val requestBody = String(requestWrapper.contentAsByteArray, Charset.forName(request.characterEncoding))
val responseBody = String(responseWrapper.contentAsByteArray, StandardCharsets.UTF_8)
logger.info("request body: $requestBody")
logger.info("response body: $responseBody ")
responseWrapper.copyBodyToResponse()
}
}
when I request POST /msg
, ApplicationRequestFilter
can log request and response body, you can also receive {"code" : "success"}
result
but I request POST /async/msg
, ApplicationRequestFilter
can log request body, couldn't log response body, you can't receive {"code" : "success"}
result
I think this is caused by asynchronous requests, response body has been written by other thread?
somebody can help resolve this problem? :)
Comment From: rstoyanchev
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.