OpenFeign / feign

Feign makes writing java http clients easier
Apache License 2.0
9.44k stars 1.92k forks source link

Fail to upload File(java.io.File) with FormEncoder + JAXRXContract #1168

Open hiasince opened 4 years ago

hiasince commented 4 years ago

I've try to use feign for uploading image file.

First, I followed the example provided on the github README file(https://github.com/OpenFeign/feign-form)

This code is working

Builder formBuilder = Feign.builder()
            .encoder(new FormEncoder())
            .decoder(new JacksonDecoder())
            .options(options)
            .logger(new Slf4jLogger())
            .requestInterceptor(new FeignRequestInterceptor())
            .logLevel(Level.HEADERS);
@RequestLine("POST /api/v2/{key}/ft/upload_image")
@Headers("Content-Type: multipart/form-data")
Response uploadImage(@HeaderMap Map<String, String> headerMap,
                     @Param("key") String key,
                     @Param("image") File image);

After then, I wanted to add and use JAXRSContract.

But this code is not working

Builder formBuilder = Feign.builder()
            .contract(new JAXRSContract())
            .encoder(new FormEncoder())
            .decoder(new JacksonDecoder())
            .options(options)
            .logger(new Slf4jLogger())
            .requestInterceptor(new FeignRequestInterceptor())
            .logLevel(Level.HEADERS);
@POST
@Path("/api/v2/{key}/ft/upload_image")
@Produces(MediaType.MULTIPART_FORM_DATA)
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response uploadImage(@PathParam("key") String key,
                     @HeaderParam("account") String account
                     @Param("image") File image);

In the case of using the above code, I got the following error message.

feign.codec.EncodeException: class java.io.File is not a type supported by this encoder.

I think those two code have to execute as same way. Is there has any problem on second code initialized with JAXRSContract? or Is there has other way to upload file using FormEncoder and JAXRSContract?

kdavisk6 commented 4 years ago

I suspect the issue is in the way the @Produces and @Consumes resolve. The FormEncoder is looking for a specific header Content-Type, with @Consumes should produce. As a way to troubleshoot, can you try removing the @Produces annotation? It doesn't really make sense from a client perspective.

hiasince commented 4 years ago

@kdavisk6 Thanks for answer my question. I removed the @Produces but nothing changed...