Feign decoder example

What you will learn here about Spring boot

  • Feign decoder example

Here we will see feign decoder with example. Feign decoder gets called whenever we are getting some exception from call API. So when we get some exception from calling API our job is to get response from the exception. Here we will see how to decode exception response sent by calling API.

Feign decoder example

Please follow the following steps to know how to use feign decoder and how to get response from exception thrown by calling API.
1)First Add following dependency in pom.xml

1)Open feign maven dependency

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

feign gson maven dependency

		
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-gson</artifactId>
</dependency>

google common io maven dependency

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>27.0.1-jre</version>
</dependency>

2)Lets assume i am getting status and reason from calling when exception is thrown by calling API, So I have created Error class which has status and reason field along with their corresponding getters and setters which is shown below
Feign decoder spring boot

3)Now I have create feign custom error decoder class which is shown below
feign custom decoder example

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.CharStreams;
import feign.Response;
import feign.codec.ErrorDecoder;
public class ApiErrorDecoder implements ErrorDecoder{

	@Override
	public Exception decode(String methodKey, Response response) {
			try {
				Reader reader=response.body().asReader(Charset.defaultCharset());
				String result = CharStreams.toString(reader);
				ObjectMapper mapper = new ObjectMapper();
				Error error=mapper.readValue(result, Error.class);
				return new RuntimeException(error.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		return new Exception(response.reason());
	}
}


4)Now we will see how to decode error response from exception which is shown below
Feign decoder example

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@RestController
public class ControllerService {
	@GetMapping("/hello")
	public Object getData() {
		Error error=null;
		try {
			//Assume you are calling third party API from here
			//whenever api returns exception then catch block will get exception 
			//from feign decoder
		}catch (Exception e) {
			try {
				ObjectMapper mapper = new ObjectMapper();
				error = mapper.readValue(e.getMessage(), Error.class);
				return error;
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
		return error;
	}
}

You may also like...