I am new to Spring Boot and I am trying to load a Json template file and replacing the placeholders with input values read from a user input file.
This is my JSON.
template.json
{
"templateId": "header",
"configurationData": {
"inboundHeaders": [
{
"key": "header1",
"value": {0}
}, {
"key": "header2",
"value": {1}
}, {
"key": "header3",
"value": {3}
}
],
"outboundHeaders": [
{
"key": "header4",
"value": {4}
}, {
"key": "header5",
"value": {5}
}, {
"key": "header6",
"value": {6}
}
]
}
}
So, here I want to replace these placeholders before sending it to remote service
public void processJson(List<String> userParams, String jsonFile){
HttpClient client = new ProductHttpClient();
mapper = new ObjectMapper();
JavaToJsonConverter converter = new JavaToJsonConverter();
RemoteServiceResponse response = null;
RequestPojo requestPojo = createRequestPojo();
List<Header> headers = new ArrayList<Header>();
headers.add(new BasicHeader("Content-Type", "application/json"));
headers.add(new BasicHeader("Authorization", "Bearer " + token.getAccessToken()));
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
String jsonRequest = MessageFormat.format(jsonFile, value1, value2, value3, value4, value5, value6); // This is the problem area which I am unable to achieve
//Working code
HttpResponse httpResponse = client.postRequest(url,
converter.convertToJson(requestPojo), urlParameters, headers);
//Not Working
HttpResponse httpResponse = client.postRequest(url,
jsonRequest, urlParameters, headers);
}
Presently, I am creating POJOs from request Json and replacing the placeholders while passing the request json to remote service. Here I see following issue
I was not able to load Json file from properties file. So, I had to generate POJOs every time for different request Json, fill object with user input value and convert those POJOs back to json string and submit to remote service.
But I just want to load the json file with placeholder and replace the placeholder with the user input values. It will save the overhead of creating so many POJOs many times.
So, can any one help on this?
Thanks in advance.