I'd like to be able to configure different json templates based on e.g. a clientId with a default template in case a template for the clientId is not defined.
Context context = new Context();
Map<String, Object> templateResolutionAttribute = new HashMap<>();
templateResolutionAttribute.put("clientId", 12345);//Trying to somehow pass a variable to the template selection process...
TemplateSpec templateSpec = new TemplateSpec(
"json/data.json",
templateResolutionAttribute
);
String output = springTemplateEngine.process(templateSpec, context);
The Spring Config class I'm using looks like this:
@Configuration
public class ThymeLeafConfig {
@Bean
public SpringResourceTemplateResolver jsonMessageTemplateResolver() {
SpringResourceTemplateResolver theResourceTemplateResolver =
new SpringResourceTemplateResolver();
theResourceTemplateResolver.setPrefix("classpath:/templates/");
theResourceTemplateResolver.setResolvablePatterns(
Collections.singleton("json/*"));
theResourceTemplateResolver.setSuffix(".json");
theResourceTemplateResolver.setCharacterEncoding("UTF-8");
theResourceTemplateResolver.setCacheable(false);
theResourceTemplateResolver.setOrder(1);
return theResourceTemplateResolver;
}
The json templates could be in different directories or alternatively have different file names (whatever works best/easiest):
json/
data.json
12345/
data.json
or
json/
data.json
data.12345.json
What is the best or easiest way to achieve returning a template that "matches" the clientId and if no match for the clientId is found return a default template?
Note: For new clients, ideally, I'd like to be able to just add new json templates without making any code changes...