我通过 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();
}
然后我得到请求
Could not find MessageBodyWriter for response object of type: java.awt.image.BufferedImage of media type: image/jpeg
是否有任何“直接方法”可以将图像放入响应中?
感谢@Robert 的指导。这里的工作代码:
@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();
}