spring-bootfreemarker

org.xml.sax.SAXParseException for freemarker template in springboot


I am trying to generate document using freemarker in springboot.

Template has:

<tr>
        <td style="width: 100%"  align='center'>
            <div class="logo-container">
                <img class="company-logo" src="${companyLogo}" alt="LOGO" align='center'" />
            </div>
        </td>   
</tr>

value for 'companylogo' is:

..../image/tmpimages.png.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20240725T043642Z&X-Amz-SignedHeaders=host&X-Amz-Expires=360000&X-Amz-Credential=xxxxxxxxxxxx%wdddeddd%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Signature=.....

This setup gives error as:

"Can't load the XML resource (using TrAX transformer). org.xml.sax.SAXParseException; lineNumber: 87; columnNumber: 180; The reference to entity \"X-Amz-Date\" must end with the ';' delimiter."

What causes this? And what's the solution?


Solution

  • Turn HTML/XML auto-escaping on! Generating HTML/XML without that is a security risk too. HTML auto-escaping will replace that & with &amp; in the output.

    How to turn on auto-escaping? Using ftlh file extension instead of ftl is the easiest way (assuming you need HTML, though it seems you are generating XML here). If you can't do that, and you can't change the defaults in the FreeMarker configuration either, then start the template with<#ftl output_format="HTML"> (or I guess <#ftl output_format="XML"> in your case).

    See also: https://freemarker.apache.org/docs/dgui_quickstart_template.html#dgui_quickstart_template_autoescaping