OpenFeign/feign

Cannot get response headers from a FeignException

Open

#1,268 创建于 2020年9月3日

在 GitHub 查看
 (2 评论) (8 反应) (0 负责人)Java (9,070 star) (1,911 fork)batch import
feedback providedhelp wanted

描述

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?

贡献者指南

Cannot get response headers from a FeignException · OpenFeign/feign#1268 | Good First Issue