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 giventype
. Invoked whenResponse.status()
is in the 2xx range and the return type is neithervoid
norResponse
. 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
Thetype
parameter will correspond to thegeneric return type
of aninterface
processed 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 byDecoder
s get wrapped in aDecodeException
unless they are a subclass ofFeignException
already, and unless the client was configured withFeign.Builder.decode404()
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
Decoder.Default
Default implementation ofDecoder
.
-
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 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 type
of 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.
-
-