Spring cloud netflix hystrix fallback not being triggered

54 views Asked by At

I'm having problems with the fallback implementation. For some reason, when my service is not running, I get error 500, and there is no fallback interception That's my implementation:

@FeignClient(name = "planner-users", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {

    @PostMapping("/user/id")
    ResponseEntity<User> findUserById(@RequestBody Long id);
}

@Component
class UserFeignClientFallback implements UserFeignClient {

    @Override
    public ResponseEntity<User> findUserById(Long id) {
        return null;
    }
}
ResponseEntity<User> result =  userFeignClient.findUserById(category.getUserId());

        if (result == null){
            return new ResponseEntity("System users doesn't work right now", HttpStatus.NOT_FOUND);
        }

        if (result.getBody() != null)
            return ResponseEntity.ok(categoryService.add(category));

        return new ResponseEntity("user id=" + category.getUserId() + " not found", HttpStatus.NOT_ACCEPTABLE);
@Component
public class FeignExceptionHandler implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {

        switch (response.status()) {
            case 406: {
                return new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, readMessage(response));
            }
        }

        return null;
    }
    private String readMessage(Response response) {

        String message = null;
        Reader reader = null;

        try {

            reader = response.body().asReader(Charset.defaultCharset());
            message = CharStreams.toString(reader);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return message;
    }

}

I have added the following dependencies:

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix:+'
implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j:+'

Also I added next conf in git property: feign.circuitbreaker.enabled=true My service reads the config, because it connected to server. I've read this question - Spring cloud openfeign 3.0.1 fallback not being triggered but it doesn't work :( This is the answer in Postman:"timestamp": "2023-10-20T16:24:43.978+00:00", "status": 500, "error": "Internal Server Error", "message": "Cannot throw exception because the return value of \"feign.ResponseHandler.decodeError(String, feign.Response)\" is null", "path": "/category/add"

0

There are 0 answers