1

Using REST with RESTEasy and Swagger, is there any way to stream data back to the caller with a GET endpoint? I've seen a couple of examples where the entire stream can be returned, but I haven't seen any examples where the data can actually be streamed back. I also did have a look at this example(followed from this link-Return File From Resteasy Server) however, this example looks like it is returning a stream and expecting the caller to utilize the stream? Is this a correct assumption?:

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/stream/test")
public Response getTestStream() {

    String myName = "name";
    InputStream stream = new ByteArrayInputStream(myName.getBytes(StandardCharsets.UTF_8));

    return Response.ok().entity(stream).build();
}

But this does not seem to work for me. I get an exception: javax.ws.rs.NotAcceptableException: RESTEASY003635: No match for accept header.

Any help would be greatly appreciated!

BigBug
  • 6,202
  • 23
  • 87
  • 138
  • 1
    Check the `accept` header of your client request, and try to set it to `application/octet-stream` . – Arnaud Aug 30 '17 at 15:49

1 Answers1

2

You can return Object of inputstream object in Response.

For e.g.

@GetMapping(value = "/stream/test")
@ResponseBody
public ResponseEntity<?> getTestStream() {

    String myName = "name";
    InputStream stream = new ByteArrayInputStream(myName.getBytes(StandardCharsets.UTF_8));

    HttpHeaders headers = new HttpHeaders();
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
    headers.add("Pragma", "no-cache");
    headers.add("Expires", "0");

    return ResponseEntity
            .ok()
            .headers(headers)
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body(new InputStreamResource(stream));
}
Yogi
  • 1,805
  • 13
  • 24