Decodes an HTTP response into a single object of the given {@code type}. Invoked when {@link Response#status()} is in the 2xx range and the return type is neither {@code void} nor {@code Response}.
Example Implementation:
public class GsonDecoder implements Decoder { private final Gson gson = new Gson(); @Override public Object decode(Response response, Type type) throws IOException { try { return gson.fromJson(response.body().asReader(), type); } catch (JsonIOException e) { if (e.getCause() != null && e.getCause() instanceof IOException) { throw IOException.class.cast(e.getCause()); } throw e; } } }
Implementation Note
The {@code type} parameter will correspond to the{@link java.lang.reflect.Method#getGenericReturnType() generic return type}of an {@link feign.Target#type() interface} processed by{@link feign.Feign#newInstance(feign.Target)}. When writing your implementation of Decoder, ensure you also test parameterized types such as {@code List
}.