Using Spring Boot I'd like to implement a mock service for an external API. As this mock is only used for testing, I'd like to keep things as simple as possible. The external API returns a JSON similar to this one:
{
"customer": {
"email": "foo@bar.com"
},
"result": {
"status_code": 100
},
"transaction": {
"amount": 100,
"currency": "EUR",
"order_no": "123456"
}
}
And the controller:
@Value("classpath:/sample.json")
private Resource sampleResource;
@GetMapping(value = "/api")
public String mockMethod() throws IOException {
final InputStream inputStream = sampleResource.getInputStream();
final String sampleResourceString = StreamUtils.copyToString(sampleResource, StandardCharsets.UTF_8);
return sampleResourceString;
}
So basically, the application loads a JSON string from a file and returns it in the response. Now I'd like to replace the amount
and the order_no
in the JSON with a dynamic value, like this:
{
"customer": {
"email": "foo@bar.com"
},
"result": {
"status_code": 100
},
"transaction": {
"amount": ${amount},
"currency": "EUR",
"order_no": "${orderNumber}"
}
}
My first idea was to use Thymeleaf for this, so I created the following configuration:
@Configuration
@RequiredArgsConstructor
public class TemplateConfiguration {
private final ApplicationContext applicationContext;
@Bean
public SpringResourceTemplateResolver templateResolver() {
final SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/templates");
templateResolver.setSuffix(".json");
templateResolver.setTemplateMode(TemplateMode.TEXT);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
}
But I'm stuck how I can actually "run" the templating so that the sampleResourceString
is replaced with the dynamic values.
Or is Thymeleaf maybe actually some kind of "overkill" for this?