spring feign client exception handling

后端 未结 3 1700
慢半拍i
慢半拍i 2021-02-09 03:01

I have some fiegn client to send request other micro service.

@FeignClient(name=\"userservice\")
public interface UserClient {

    @RequestMapping(
                    


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-02-09 03:59

    Not the same issue, but this helped in my situation. OpenFeign's FeignException doesn't bind to a specific HTTP status (i.e. doesn't use Spring's @ResponseStatus annotation), which makes Spring default to 500 whenever faced with a FeignException. That's okay because a FeignException can have numerous causes that can't be related to a particular HTTP status.

    However you can change the way that Spring handles FeignExceptions. Simply define an ExceptionHandler that handles the FeignException

    @RestControllerAdvice
    public class GlobalExceptionHandler {
    
        @ExceptionHandler(FeignException.class)
        public String handleFeignStatusException(FeignException e, HttpServletResponse response) {
            response.setStatus(e.status());
            return "feignError";
        }
    
    }
    

    This example makes Spring return the same HTTP status that you received

提交回复
热议问题