1

I am working on a Spring Boot project using RestTemplate in order to perform GET call to an external API in order to retrieve information.

I have the following situation and a doubt about what could be a smart and elegant way to map the field of the retrieved JSON response into a Java model object having a totally different structure. I know that I can create a brand new model object having the same structure of the response but it is not convenient in my use case because then I have to check that this retrieved object contains the same information of another object caming from another API, so I need a single format.

Basically I have this method performing the API call:

@Override
public NotaryDistrictDetails getNotaryDistrictDetails(String districtId) throws URISyntaxException {
    
    String completeURL =  this.wpPortalGetNotaryDistrictPostDetailsURL.replace("{districtId}", districtId);
    System.out.println("completeURL: " + completeURL);
    
    URI uri = new URI(completeURL);
    System.out.println(uri);
    
    try {
        HttpHeaders basicAuthHeader =  this.getWpBasicAuthenticationHeader();
        
        HttpEntity<String> request = new HttpEntity<String>(basicAuthHeader);
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, request, String.class);
        System.out.println("RESPONSE STATUS: " + response.getStatusCodeValue());
        System.out.println(response.getBody());

    }
    catch(Exception e) {
        System.out.println("Exception: " + e);
    }
    
    return null;
}

This return a JSON like this:

{
    "id": 38803,
    "date": "2021-09-11T10:53:32",
    "date_gmt": "2021-09-11T10:53:32",
    "guid": {
        "rendered": "https://www.XXX.it/notary/cc/"
    },
    "modified": "2021-09-11T10:53:32",
    "modified_gmt": "2021-09-11T10:53:32",
    "slug": "cc",
    "status": "publish",
    "type": "notary",
    "link": "https://www.XXX.it/notary/cc/",
    "title": {
        "rendered": "CC"
    },
    "template": "",
    "meta": [],
    "toolset-meta": {
        "gruppo-storico-notary": {
            "sede-1-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-2-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-3-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-4-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-5-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-6-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-7-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-8-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-9-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-10-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-11-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-12-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-13-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-14-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sede-15-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-1-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-2-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-3-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-4-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-5-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-6-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-7-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-8-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-9-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-10-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-11-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-12-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-13-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-14-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "dal-15-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-1-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-2-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-3-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-4-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-5-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-6-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-7-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-8-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-9-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-10-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-11-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-12-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-13-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-14-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "note-15-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-1-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-2-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-3-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-4-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-5-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-6-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-7-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-8-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-9-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-10-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-11-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-12-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-13-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-14-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "stato-sede-15-notary": {
                "type": "textfield",
                "raw": "AA"
            }
        },
        "gruppo-contatti-notary": {
            "indirizzo-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "cap-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "numero-telefono-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "fax-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "mail-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "mail-pec-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "url-notary": {
                "type": "url",
                "raw": "AA"
            }
        },
        "gruppo-scheda-notary": {
            "idnotary": {
                "type": "textfield",
                "raw": "BB"
            },
            "nome-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "cognome-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "sesso-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "codice-fiscale-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "regione-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "provincia-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "localita-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "distretto-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "distretto-cf-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "informazioni-notary": {
                "type": "textfield",
                "raw": "AA"
            },
            "data-nascita-notary": {
                "type": "textfield",
                "raw": "2019-02-22"
            }
        }
    },
    "_links": {
        "self": [
            {
                "href": "https://www.XXX.it/wp-json/wp/v2/notary/38803"
            }
        ],
        "collection": [
            {
                "href": "https://www.XXX.it/wp-json/wp/v2/notary"
            }
        ],
        "about": [
            {
                "href": "https://www.XXX.it/wp-json/wp/v2/types/notary"
            }
        ],
        "wp:attachment": [
            {
                "href": "https://www.XXX.it/wp-json/wp/v2/media?parent=38803"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
}

Starting from the retrieved JSON I have to build a model object using only some of the fields contained in the previous response.

I know that I can access to these fields using Jackson JsonNode object, basically something like this:

ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, request, String.class);

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(response.getBody());

this.myModelObj.setId(jsonNode.get(0).get("id").toString());
// SET ALL THE OTHER FIELD OF MY MODEL OBJECT

So basically my original idea was to use Jackson JsonNode in order to manually retrieve all the field from the response and then manually create my model object.

Could be the correct way to approach this kind of problem or exist better way?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

0 Answers0