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


~ /home 🏠 ~ talks 💬 ~ bash ~ craftsmanship ~ db ~ dongxi ~ emacs ~ escenic ~ iam ~ java ~ js ~ language ~ latex ~ ldap ~ life ~ linux ~ llm ~ mac-os-x ~ mt-foo ~ network ~ norsk ~ python ~ quotes ~ running ~ security ~ travel ~ unix ~ various ~ vcs ~ webdesign ~ windows ~ discoveries ~ cv 🧙 ~

Licensed under CC BY Creative Commons License ~ 📡 RSS feed ~ ✉ torstein.k.johansen @ gmail ~ 🐘 @skybert@hachyderm.io ~ 🎥 youtube.com/@skybert