Alright then, one way of doing it would be configuring the Thymeleaf engine in a single method. For example:
@GetMapping("/xml")
public void xml(HttpServletResponse res) throws Exception {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(new AnnotationConfigApplicationContext());
resolver.setPrefix("classpath:/xml/");
resolver.setSuffix(".xml");
resolver.setCharacterEncoding("UTF-8");
resolver.setTemplateMode(TemplateMode.XML);
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(resolver);
Context ctx = new Context();
ctx.setVariable("notes", Arrays.asList("one note", "two note"));
String xml = engine.process("template", ctx);
res.setHeader("Content-Disposition", "attachment; filename=template.xml");
res.setContentType("application/xml");
PrintWriter writer = res.getWriter();
writer.print(xml);
writer.close();
}
Where the template is located in src/main/resources/xml/template.xml
. You set your model variables using the ctx.setVariable()
method.