I’m looking for a way to automatically have my RESTful controller, which returns a String, output a pdf file to the calling browser, when the URI has a .pdf at the end:
http://localhost:9090/services-rs/notices/58357.pdf
Without the .pdf at the end, it currently merely returns the String, i.e. plain text, in the browser.
I tried adding:
<entry key="pdf" value="application/pdf"/>
to my mediatypes list in my ContentNegotiatingViewResolver, but evidently that didn’t do the trick.
How do I go about this? Is there a 3rd party library I need to use, or does Spring MVC have this ability built-in?
The ContentNegotiatingViewResolver does not use the extension at the end of your URI. It uses the Accept header in the request. If you would like to use the ContentNegotiatingViewResolver, ensure that your Accept header on the client side is using application/pdf.
If this is not possible for you, you will need to have a different controller.