添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

When using Feign client in Spring Boot, error messages can sometimes be cryptic and difficult to understand. However, by decoding these error messages, you can pinpoint the issue and resolve it efficiently.

Using a custom error decoder in Feign allows you to intercept and handle error responses from the target service in a more customized way. You can inspect the HTTP status code, response body, and headers to decode the error message and take appropriate actions based on the error type. Here’s how to implement a custom error decoder in Spring Boot with Feign:

import feign.Response;
import feign.codec.ErrorDecoder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableFeignClients
public class LearnSpringBootOnlineErrorDecoder {
    public static void main(String[] args) {
        SpringApplication.run(LearnSpringBootOnlineErrorDecoder.class, args);
    @Bean
    public ErrorDecoder errorDecoder() {
        return new CustomErrorDecoder();

CustomErrorDecoder implements Feign’s ErrorDecoder interface and overrides the decode method to customize error handling logic based on HTTP status codes.

import feign.Response;
import feign.codec.ErrorDecoder;
public class CustomErrorDecoder implements ErrorDecoder {
    @Override
    public Exception decode(String methodKey, Response response) {
        // Decode error response and return custom exception based on status code or response body
        switch (response.status()) {
            case 400:
                return new BadRequestException("Bad request");
            case 401:
                return new UnauthorizedException("Unauthorized");
            case 404:
                return new ResourceNotFoundException("Resource not found");
            default:
                return new Exception("Unknown error occurred");

Custom exceptions such as BadRequestException, UnauthorizedException, and ResourceNotFoundException are created to represent different types of errors. These custom exceptions can be thrown by the custom error decoder based on the decoded error response. When using Feign clients in your application, exceptions thrown by the custom error decoder will be propagated and can be caught and handled appropriately in your code.

public class BadRequestException extends RuntimeException {
    public BadRequestException(String message) {
        super(message);
public class UnauthorizedException extends RuntimeException {
    public UnauthorizedException(String message) {
        super(message);
public class ResourceNotFoundException extends RuntimeException {
    public ResourceNotFoundException(String message) {
        super(message);

Conclusion

In conclusion, custom error decoding in Feign client within Spring Boot allows for precise handling of error responses from target services. By implementing a custom error decoder and registering it within the application configuration, you can extract meaningful error information and throw custom exceptions. This approach enhances error handling, providing more clarity and control when dealing with errors in Feign client interactions.

Explore our diverse collection of blogs covering a wide range of topics here.