Package feign.codec

Interface Decoder

  • All Known Implementing Classes:
    Decoder.Default, OptionalDecoder, StreamDecoder, StringDecoder

    public interface Decoder
    Decodes an HTTP response into a single object of the given type. Invoked when Response.status() is in the 2xx range and the return type is neither void nor 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 type parameter will correspond to the generic return type of an interface processed by Feign.newInstance(feign.Target). When writing your implementation of Decoder, ensure you also test parameterized types such as List<Foo>.

    Note on exception propagation

    Exceptions thrown by Decoders get wrapped in a DecodeException unless they are a subclass of FeignException already, and unless the client was configured with Feign.Builder.decode404().
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Decoder.Default
      Default implementation of Decoder.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.lang.Object decode​(Response response, java.lang.reflect.Type type)
      Decodes an http response into an object corresponding to its generic return type.
    • Method Detail

      • decode

        java.lang.Object decode​(Response response,
                                java.lang.reflect.Type type)
                         throws java.io.IOException,
                                DecodeException,
                                FeignException
        Decodes an http response into an object corresponding to its generic return type. If you need to wrap exceptions, please do so via DecodeException.
        Parameters:
        response - the response to decode
        type - generic return type of the method corresponding to this response.
        Returns:
        instance of type
        Throws:
        java.io.IOException - will be propagated safely to the caller.
        DecodeException - when decoding failed due to a checked exception besides IOException.
        FeignException - when decoding succeeds, but conveys the operation failed.