0

I have a REST Java service which returns a file as a stream. It's similar to the output approaches described here:

Input and Output binary streams using JERSEY?

I also use StreamingOuput as described above.

I now wonder now how to call this method on the client side. I use Swagger but I am not very good with it, and I have no idea how to call my method.

Currently in the web service I have defined

@ApiOperation(value = "Export file from the database", response = File.class)

but I doubt this File.class is a good idea here.

Do I need to modify this response type? What is a good way of calling my service?

When I call my service from the Swagger UI interface I see these response headers.

{ "date": "Sun, 27 Aug 2017 11:14:20 GMT", "content-disposition": "attachment; filename=\"DealerTexts.xlsx\"", "x-powered-by": "Servlet/3.1 JSP/2.3 ( Java/Oracle Corporation/1.8)", "transfer-encoding": "chunked", "server": "", "content-type": "application/octet-stream" }

In the response body it gives me a link which I can click and download the file generated by the web service. So my question is just how I can call this same thing from Java code on the client side.

halfer
  • 19,824
  • 17
  • 99
  • 186
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • The swagger codegen should generare the client code for you. Anyway it should be a simple HTTP GET request in Java. – Mario Santini Aug 27 '17 at 11:19
  • @MarioSantini Right, but what do I define on the client side as response attribute of @ApiOperation? `response = File.class` is probably not good so this confuses Swagger to generate the right code on the client side, I think. – peter.petrov Aug 27 '17 at 11:20
  • 1
    I'm not expert of swagger but googling I found this: https://github.com/swagger-api/swagger-core/issues/1876. You should try to write an example in code and test it to see what happen. If you get an error update your question accordingly. – Mario Santini Aug 27 '17 at 14:53
  • @MarioSantini Yes, I managed to get it working myself with "byte.class". I didn't find that link though, I just did it with trial and error and with some intuition. So your link now confirms my approach. I am all set now. Thank you. – peter.petrov Aug 27 '17 at 19:06

0 Answers0