Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Created March 19, 2015 10:42
Show Gist options
  • Save michalbcz/00a069c7a2542ea7fa2a to your computer and use it in GitHub Desktop.
Save michalbcz/00a069c7a2542ea7fa2a to your computer and use it in GitHub Desktop.
jax-rs (Resteasy) - dynamic image resource sample
@GET
@Path("/images/{id}")
@Produces("image/png")
public Response getImages(final @PathParam("id") String id) {
StreamingOutput imageStreamingOutput = new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
final BufferedImage image = new BufferedImage(250, 250, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setFont(g.getFont().deriveFont(30f));
g.drawString(id, 100, 100);
g.dispose();
ImageIO.write(image, "png", output);
}
};
return Response.ok(imageStreamingOutput, MediaType.valueOf("image/png")).build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment