Q:

Spring Cloud Open Feign: Decoder for ByteArrayResource

I have a Spring Boot Rest End Point defined in an interface to download an image
@GetMapping(value = "/{name}")
ResponseEntity<ByteArrayResource> getFileByName(@PathVariable("name") String name);

And I use Feign Builder to invoke this end point.
Feign.builder()
    .client(new ApacheHttpClient())
    .contract(new SpringMvcContract())
    .decoder(new JacksonDecoder())
    .encoder(new JacksonEncoder())  
    .target(clazz, url)

On invoking, I get below error
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('�' (code 65533 / 0xfffd)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: (BufferedReader); line: 1, column: 2]

When I try to invoke the end point directly from Insomnia, it works fine. But fails through Feign Builder. The response content type is image/jpeg
Is there any specific decoder in feign to handle ByteArrayResource? I tried ResponseEntityDecoder, StreamDecoder and JacksonDecoder. None of it works.
On debugging, I see that Jackson ObjectMapper readValue fails. I tried changing the return type from ByteArraySource to byte[], didn't work either.
Any help?

A:

I wrote my own little decoder and the problem was resolved. Below is the decoder
private Decoder byteArrayResourceDecoder() {
        Decoder decoder = (response, type) -> {
            if (type instanceof Class && ByteArrayResource.class.isAssignableFrom((Class) type)) {
                return StreamUtils.copyToByteArray(response.body().asInputStream());
            }
            return new JacksonDecoder().decode(response, type);
        };

        return new ResponseEntityDecoder(decoder);
    }

Hope this template helps others who has similar issues. Would have expected Feign to have decoder that supports all return types.

