Package feign.codec
Interface Decoder
-
- All Known Implementing Classes:
Decoder.Default,OptionalDecoder,StreamDecoder,StringDecoder
public interface DecoderDecodes an HTTP response into a single object of the giventype. Invoked whenResponse.status()is in the 2xx range and the return type is neithervoidnorResponse. 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
Thetypeparameter will correspond to thegeneric return typeof aninterfaceprocessed byFeign.newInstance(feign.Target). When writing your implementation of Decoder, ensure you also test parameterized types such asList<Foo>.
Note on exception propagation
Exceptions thrown byDecoders get wrapped in aDecodeExceptionunless they are a subclass ofFeignExceptionalready, and unless the client was configured withFeign.Builder.decode404().
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classDecoder.DefaultDefault implementation ofDecoder.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.Objectdecode(Response response, java.lang.reflect.Type type)Decodes an http response into an object corresponding to itsgeneric 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 itsgeneric return type. If you need to wrap exceptions, please do so viaDecodeException.- Parameters:
response- the response to decodetype-generic return typeof the method corresponding to thisresponse.- 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.
-
-