1

I have a Rest Template exchange for get service. The service returns the responses as below :

Response1(Single Document):- { "irpage": [{ "drawer": "HPPS", "folderNumber": "HPA00008047642", "docID": "1", "pageNumber": "1", "din": "HPPSHPA00008047642000001", "userKey1": "20151119NJ", "docType": "APPD", "docDate": "20151119", "numberPages": "1", "fileName": "\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845", "tempFileName": "\\prcins.net\Shared\irtestnjimages01\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845.pdf", "driver": "", "dateCaptured": "20151119", "timeCaptured": "08:45:04", "singleInd": "S", "userID": "SCHEDL2", "status": "U", "batch": "Z000", "format": "PDF", "media": "D", "orientation": "0", "tempDin": "12201511190845952490845SCHEDL2", "transType": "APPD", "operator": "", "userkey2": "0", "reason": "Computer Generated Document", "archiveStatus": "A", "archiveDate": "20151119", "deviceID": "1", "packageID": "1", "packageType": "15007", "markedInd": "0", "dspPageNumber": "1", "aMedia": "", "aDrive": "", "description": "Output", "folderName": "GAIL BARTLING", "docDescription": "Application Document", "overlayID": null, "docIndex": null }] }

Response2(Multiple Documents): { "irpage": [{ "drawer": "HPPS", "folderNumber": "HPA00008047642", "docID": "1", "pageNumber": "1", "din": "HPPSHPA00008047642000001", "userKey1": "20151119NJ", "docType": "APPD", "docDate": "20151119", "numberPages": "1", "fileName": "\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845", "tempFileName": "\\prcins.net\Shared\irtestnjimages01\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845.pdf", "driver": "", "dateCaptured": "20151119", "timeCaptured": "08:45:04", "singleInd": "S", "userID": "SCHEDL2", "status": "U", "batch": "Z000", "format": "PDF", "media": "D", "orientation": "0", "tempDin": "12201511190845952490845SCHEDL2", "transType": "APPD", "operator": "", "userkey2": "0", "reason": "Computer Generated Document", "archiveStatus": "A", "archiveDate": "20151119", "deviceID": "1", "packageID": "1", "packageType": "15007", "markedInd": "0", "dspPageNumber": "1", "aMedia": "", "aDrive": "", "description": "Output", "folderName": "GAIL BARTLING", "docDescription": "Application Document", "overlayID": null, "docIndex": null }, { "drawer": "HPPS", "folderNumber": "HPA00008047642", "docID": "4", "pageNumber": "1", "din": "HPPSHPA00008047642000004", "userKey1": "20160104NJ", "docType": "FINA", "docDate": "20160104", "numberPages": "1", "fileName": "\12_HPPS\20160105\5_Z000\SCHEDL2\308070010819", "tempFileName": "\\prcins.net\Shared\irtestnjimages01\12_HPPS\20160105\5_Z000\SCHEDL2\308070010819.pdf", "driver": "", "dateCaptured": "20160105", "timeCaptured": "08:19:30", "singleInd": "S", "userID": "SCHEDL2", "status": "U", "batch": "Z000", "format": "PDF", "media": "D", "orientation": "0", "tempDin": "12201601050819308070819SCHEDL2", "transType": "FINA", "operator": "", "userkey2": "0", "reason": "Computer Generated Document", "archiveStatus": "A", "archiveDate": "20160105", "deviceID": "1", "packageID": "4", "packageType": "15007", "markedInd": "0", "dspPageNumber": "1", "aMedia": "", "aDrive": "", "description": "Output", "folderName": "GAIL BARTLING", "docDescription": "Final Notice of Cancellation", "overlayID": null, "docIndex": null }] }

When I provide the Class < E > targetClass as new HashMap< String, List< Map < String , String>>>().getClass() the Response2 get parsed and I get the results in a Map, however the response1(Single Document) doesn't work and returns as null.

or If I provide the Class < E > targetClass as new HashMap< String , Map < String, String>>().getClass() the Response1 get parsed and I get the results in a Map, however the response2(Multiple Documents) doesn't work and returns as null.

I am not sure how to handle and parse the both the responses from service ?

responseMap =  restUtils.findDocuments(urlBuilder.build(),new HashMap<String,List<Map<String,String>>>().getClass());


**RestUtils Class:** 
public <E> E findDocuments(String url,Class<E> targetClass) throws JsonProcessingException {

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
    headers.add(ACCEPT, MediaType.APPLICATION_JSON_VALUE);
    headers.add(CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE);
    //headers.add("id", StringUtils.trimToEmpty(policyNo));
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
    ResponseEntity<E> response = restTemplate.exchange(url,HttpMethod.GET, requestEntity, targetClass);
    ObjectMapper mapper = new ObjectMapper();
    String jsonResponse = mapper.writeValueAsString(response); 
    System.out.println(jsonResponse);

    return (E) response.getBody();
}

Any help will be appreciated.

2
  • Terrible formating. Please fix it ;) Commented Mar 11, 2016 at 21:53
  • Don't know how to format json on SO editor, tried to put in a formatted Json but it make a mess of it Commented Mar 12, 2016 at 4:10

1 Answer 1

2

Response1(Single Document):-

{  
 "irpage":[  
  {  
     "drawer":"HPPS",
     "folderNumber":"HPA00008047642",
     "docID":"1",
     "pageNumber":"1",
     "din":"HPPSHPA00008047642000001",
     "userKey1":"20151119NJ",
     "docType":"APPD",
     "docDate":"20151119",
     "numberPages":"1",
     "fileName":"\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845",
     "tempFileName":"\\prcins.net\Shared\irtestnjimages01\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845.pdf",
     "driver":"",
     "dateCaptured":"20151119",
     "timeCaptured":"08:45:04",
     "singleInd":"S",
     "userID":"SCHEDL2",
     "status":"U",
     "batch":"Z000",
     "format":"PDF",
     "media":"D",
     "orientation":"0",
     "tempDin":"12201511190845952490845SCHEDL2",
     "transType":"APPD",
     "operator":"",
     "userkey2":"0",
     "reason":"Computer Generated Document",
     "archiveStatus":"A",
     "archiveDate":"20151119",
     "deviceID":"1",
     "packageID":"1",
     "packageType":"15007",
     "markedInd":"0",
     "dspPageNumber":"1",
     "aMedia":"",
     "aDrive":"",
     "description":"Output",
     "folderName":"GAIL BARTLING",
     "docDescription":"Application Document",
     "overlayID":null,
     "docIndex":null
  }
 ]
}

Response2(Multiple Documents)

{  
 "irpage":[  
  {  
     "drawer":"HPPS",
     "folderNumber":"HPA00008047642",
     "docID":"1",
     "pageNumber":"1",
     "din":"HPPSHPA00008047642000001",
     "userKey1":"20151119NJ",
     "docType":"APPD",
     "docDate":"20151119",
     "numberPages":"1",
     "fileName":"\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845",
     "tempFileName":"\\prcins.net\Shared\irtestnjimages01\12_HPPS\20151119\5_Z000\SCHEDL2\952490010845.pdf",
     "driver":"",
     "dateCaptured":"20151119",
     "timeCaptured":"08:45:04",
     "singleInd":"S",
     "userID":"SCHEDL2",
     "status":"U",
     "batch":"Z000",
     "format":"PDF",
     "media":"D",
     "orientation":"0",
     "tempDin":"12201511190845952490845SCHEDL2",
     "transType":"APPD",
     "operator":"",
     "userkey2":"0",
     "reason":"Computer Generated Document",
     "archiveStatus":"A",
     "archiveDate":"20151119",
     "deviceID":"1",
     "packageID":"1",
     "packageType":"15007",
     "markedInd":"0",
     "dspPageNumber":"1",
     "aMedia":"",
     "aDrive":"",
     "description":"Output",
     "folderName":"GAIL BARTLING",
     "docDescription":"Application Document",
     "overlayID":null,
     "docIndex":null
  },
  {  
     "drawer":"HPPS",
     "folderNumber":"HPA00008047642",
     "docID":"4",
     "pageNumber":"1",
     "din":"HPPSHPA00008047642000004",
     "userKey1":"20160104NJ",
     "docType":"FINA",
     "docDate":"20160104",
     "numberPages":"1",
     "fileName":"\12_HPPS\20160105\5_Z000\SCHEDL2\308070010819",
     "tempFileName":"\\prcins.net\Shared\irtestnjimages01\12_HPPS\20160105\5_Z000\SCHEDL2\308070010819.pdf",
     "driver":"",
     "dateCaptured":"20160105",
     "timeCaptured":"08:19:30",
     "singleInd":"S",
     "userID":"SCHEDL2",
     "status":"U",
     "batch":"Z000",
     "format":"PDF",
     "media":"D",
     "orientation":"0",
     "tempDin":"12201601050819308070819SCHEDL2",
     "transType":"FINA",
     "operator":"",
     "userkey2":"0",
     "reason":"Computer Generated Document",
     "archiveStatus":"A",
     "archiveDate":"20160105",
     "deviceID":"1",
     "packageID":"4",
     "packageType":"15007",
     "markedInd":"0",
     "dspPageNumber":"1",
     "aMedia":"",
     "aDrive":"",
     "description":"Output",
     "folderName":"GAIL BARTLING",
     "docDescription":"Final Notice of Cancellation",
     "overlayID":null,
     "docIndex":null
  }
 ]
}

I'd solve it this way

        Gson gson = new GsonBuilder().create();
        responseObject = gson.fromJson(jsonString, MyClass.class);

MyClass

public class MyClass {
    List<ArrayEntry> irpage;

    public class ArrayEntry {
        String drawer;
        String folderNumber;
        ...
    }
}

Use modifiers that you need (public/private)

Maven dependency for Gson

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.4</version>
    </dependency>
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.