1

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.

Ashish
  • 341
  • 2
  • 7
  • 17
  • 2
    Please dont describe your code, show us your code and probability of you getting help will be much greater. – Toerktumlare Jan 03 '20 at 15:02
  • Thanks Ok let me see if I can. – Ashish Jan 03 '20 at 15:07
  • Values like `{0}` make `JSON` invalid. You need to choose another way how to represent placeholder, maybe by using `String` primitive like `"{0}"`. You can use also [Formatter](https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html), see also [Understanding the $ in Java's format strings](https://stackoverflow.com/questions/1915074/understanding-the-in-javas-format-strings) – Michał Ziober Jan 03 '20 at 20:19
  • Thanks @MichalZiober but will it not make things more complex and how would the placeholders be placed? – Ashish Jan 06 '20 at 02:33

0 Answers0