説明
TL;DR: Is it possible to retrieve response headers from a FeignException?
My use case is this: I'm using Feign from microservice A to call microservice B. If the call fails with a 4xx status code (which results in a FeignClientException being thrown), the response may contain information about what went wrong, which fields were missing in the request, etc, so I'd like to relay the response to the caller of microservice A. I've created a Spring exception handler for this purpose:
@RestControllerAdvice
class FeignExceptionHandler {
@ExceptionHandler(FeignException.FeignClientException::class)
fun handleFeignClientException(e: FeignException.FeignClientException): ResponseEntity<String> {
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON
return ResponseEntity(e.contentUTF8(), headers, HttpStatus.valueOf(e.status()))
}
}
The thing is, that the response content type may not always be application/json, so I'd like to fetch the content type from the response itself (if it's available). However, I found no easy way to retrieve the response headers from a FeignException, only the response body is available. Am I missing something obvious here? Or maybe is there a better way to achieve what I'm trying to do?