#Returning a Buffered Image in Spring boot

20 messages · Page 1 of 1 (latest)

arctic vale
#

Hey friends, I keep getting an error when I try to return a buffered image with spring. Here is the error below:
2023-03-14T13:45:04.645-04:00 WARN 81359 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.awt.image.BufferedImage] with preset Content-Type 'null']

Here is the GetMapping code.

@GetMapping(path = "/long", produces = "image/png")
    public BufferedImage createLong() {
        try {
            return ImageIO.read(new File("/Users/ntdi/Documents/planium/src/main/resources/static/Ntdi_world_1200px-01.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
}```
viscid narwhalBOT
#

This post has been reserved for your question.

Hey @arctic vale! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

arctic vale
#

Im just unsure of how to return a buffered image, I did some research and it could be a content header thing? This seems silly.. 😦

forest anchor
arctic vale
#

instead of returning a buffered image, I am returning a link to where the image would be located

#

I am storing it in a dir inside of the resources/static folder

#

but now I cannot access the image live until the spring boot app restarts bc the images there arent updated live

forest anchor
arctic vale
#

this allows spring web to constantly reload, so maybe it'll work?

forest anchor
#

This is not a great solution. I would recommend putting it in a regular directory like /tmp/myapp or whatever

#

Or /opt/myapp or what have you (or the windows equivalent)

arctic vale
#

I know it isn't great, but I just tested it and it works.

#

¯_(ツ)_/¯

forest anchor
#

Well if it's good enough for you who am I to criticise

arctic vale
#

haha, Thanks a lot!

viscid narwhalBOT
# arctic vale haha, Thanks a lot!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.