JAX-RS resource that returns application/zip


Option 1

Use standard media type that JAX-RS has a serializer for (through Jackson) and set application/zip when we do the actual streaming:

@Produces(MediaType.APPLICATION_JSON)
public Response getZip() {

  StreamingOutput output = stream -> {
    try (ZipOutputStream zipOutput = new ZipOutputStream(stream)) {
      // write to zipOut
      zipOutput.flush();
    }
  };
  return Response.ok(output)
      .header(HttpHeaders.CONTENT_TYPE, "application/zip")
      .header(
          CONTENT_DISPOSITION,
          "attachment; filename=\"file.zip\"")
      .build();
}

Option 2


Licensed under CC BY Creative Commons License ~ ✉ torstein.k.johansen @ gmail ~ 🐘 @skybert@emacs.ch ~ 🐦 @torsteinkrause