I know that you can do this
<img src="http://some_svg_on_the_web" />
But what if I have a controller method annotated with @ResponseBody
@RequestMapping(value="getSVG")
public @ResponseBody String getSVG(HttpServletRequest request, HttpServerletResponse response) {
String SVG = // build the SVG XML as a string
return SVG;
}
Can I then say
<img src="/getSVG" />
I have tested and the controller is definitely being hit but no image is being shown on the page.
I believe the problem is Spring is setting the default content type to
application/octet-stream
and the browser can't read your XML as that. Instead, you need to actually set theContent-Type
header, either through theHttpServerletResponse
or with Spring'sResponseEntity
.The fact that the you have the XML as a
String
doesn't really matter, you could've usedgetBytes()
to make the contentbyte[]
. You can also use theResource
class to have Spring get the bytes directly from a classpath or file system resource. You would parameterizeResponseEntity
accordingly (there are a number of supported types).