Original Image in RESTeasy

I have this service with RESTeasy:

@GET @Path("/{name}") @Produces("image/jpeg") public BufferedImage get(@PathParam("name") String name) { Monitor m = Monitor.getMonitor(name); if (m == null) { return null; } return m.getImage(); } 

then I get after request

 Could not find MessageBodyWriter for response object of type: java.awt.image.BufferedImage of media type: image/jpeg 

Is there any “direct way” to get the image in response?


Thanks @Robert for the directions. Here's the working code:

 @GET @Path("/{name}") @Produces("image/jpeg") public byte[] get(@PathParam("name") String name) { Monitor m = Monitor.getMonitor(name); if (m == null) { return null; } ByteArrayOutputStream bo = new ByteArrayOutputStream(2048); try { ImageIO.write(m.getImage(),"jpeg",bo); } catch (IOException ex) { return null; } return bo.toByteArray(); } 
+7
source share
1 answer

You must try

  • encode BufferedImage as jpg. Take a look at the javax.imageio.ImageIO class
  • declare your method to return byte[]
  • make sure that your application will always run on a server that does not start using java.awt.headless=true (i.e. there is no graphical subsystem)

Please let us know if this works, because I have no idea whether I will do it and will not be able to try it myself right now.

+8
source

All Articles