rest - SprintBoot returning a PNG from a Controller's RequestMapping -
i've been scouring internet resources , feel have answer, can't quite seem bufferedimage
returned browser window.
the project generates maze can create bufferedimage
.
here code controller.
@requestmapping(method = requestmethod.get, path = "/image", params = {"rows", "columns"}) public responseentity<byte[]> image(@requestparam(name = "rows") int rows, @requestparam(name = "columns") int columns) throws ioexception, interruptedexception { try { basiccartesiangrid requestedmaze = new basiccartesiangrid(rows, columns); requestedmaze.foreach(cellalgorithms.binary_tree); bufferedimage bufferedimage = requestedmaze.todisplayimage(); { // dumping file debugging <- works expected file outputfile = new file("save.png"); imageio.write(bufferedimage, "png", outputfile); } bytearrayoutputstream pngbytestream = new bytearrayoutputstream(); imageio.write(bufferedimage, "png", pngbytestream); byte[] pngbytes = pngbytestream.tobytearray(); final httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.image_png); headers.setcontentlength(pngbytes.length); headers.setcachecontrol(cachecontrol.nocache().getheadervalue()); return new responseentity<>(pngbytes, headers, httpstatus.ok); } catch (exception e) { // hasn't occurred yet, in case thread.sleep(1000); system.err.println(e.getlocalizedmessage()); final httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.text_plain); return new responseentity<>(e.getlocalizedmessage().getbytes("ascii"), headers, httpstatus.internal_server_error); } }
i have ascertained png being generated correctly, file exists , viewable on hard-drive. browser gets broken image returned back. terminal, can more information.
curl "http://localhost:8080/maze/image?rows=10&columns=10"
dumps out following (the quotation marks part of response, while data represented ellipsis changes request request, due fact each maze randomly generated , unique):
"ivborw0kggoaaaansuheugaaa......"
i googled string prefix, , found this page. shows string should used data-uri, so:
<img src="data:image/png;base64,ivborw0kggoaaaansuheugaaa…" >
i'm not sure go here. seems image being generated correctly, must missing header in response tell browser/spring these bytes should interpreted image , not string.
update: based on dialog between myself , shawn clark
answer section, here have presently.
@springbootapplication @log4j public class springmazesapplication { @bean public httpmessageconverter<bufferedimage> bufferedimagehttpmessageconverter() { log.debug("registering bufferedimage converter"); return new bufferedimagehttpmessageconverter(); } public static void main(string[] args) throws ioexception { springapplication.run(springmazesapplication.class, args); } }
and actual controller:
@controller @requestmapping(path = "/maze/basic", method = requestmethod.get) @log4j public class basicmazecontroller { @requestmapping(params = {"format", "format=text"}, produces = mediatype.text_plain_value) @responsebody public string simplemazetext(@requestparam(name = "rows", defaultvalue = "10", required = false) int rows, @requestparam(name = "columns", defaultvalue = "10", required = false) int columns) throws ioexception { basiccartesiangrid requestedmaze = new basiccartesiangrid(rows, columns); requestedmaze.foreach(cellalgorithms.binary_tree); return requestedmaze.todisplaystring(); } @requestmapping(params = {"format=image"}, produces = mediatype.image_png_value) @responsebody public bufferedimage simplemazeimage(@requestparam(name = "rows", defaultvalue = "10", required = false) int rows, @requestparam(name = "columns", defaultvalue = "10", required = false) int columns) throws ioexception { log.debug("starting image generation"); basiccartesiangrid requestedmaze = new basiccartesiangrid(rows, columns); requestedmaze.foreach(cellalgorithms.binary_tree); bufferedimage bufferedimage = requestedmaze.todisplayimage(); { // dumping file debugging <- works expected log.debug("dumping image hd"); file outputfile = new file("save.png"); imageio.write(bufferedimage, "png", outputfile); } log.debug("returning image generation"); return bufferedimage; } @requestmapping @responsebody public responseentity<string> simplemazeinvalid(@requestparam(name = "rows", defaultvalue = "10", required = false) int rows, @requestparam(name = "columns", defaultvalue = "10", required = false) int columns, @requestparam(name = "format") string format) throws ioexception { final httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.text_plain); return new responseentity<>("invalid format: " + format, headers, httpstatus.bad_request); } }
from terminal can curl -d - "url"
, can see both logging/debugging , output terminal, converter registered @ begging of application , i'm getting responses expect actual image uri returns 406 not acceptable
. if remove @responsebody
image method, returns 500
. can verify image generated being written disk expect should.
check out produces
attribute on @requestmapping
. want set image/png
.
here complete example:
@restcontroller public class produceimage { @getmapping(path = "/image", produces = "image/png") public bufferedimage image() throws exception { bufferedimage bufferedimage = imageio.read(new file("e:\\downloads\\skin_201305121633211421.png")); return bufferedimage; } }
my bufferedimage computer can bufferedimage have requestedmaze.todisplayimage()
without having other work. make work want include bufferedimagehttpmessageconverter
in context.
@bean public httpmessageconverter<bufferedimage> bufferedimagehttpmessageconverter() { return new bufferedimagehttpmessageconverter(); }
Comments
Post a Comment