我有一个Rest Template交换以获得服务。该服务返回如下响应:

回应1(单一文件):-
    {
    “ irpage”:[{
        “抽屉”:“ HPPS”,
        “ folderNumber”:“ HPA00008047642”,
        “ docID”:“ 1”,
        “ pageNumber”:“ 1”,
        “ din”:“ HPPSHP​​A00008047642000001”,
        “ 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”,
        “批”:“ Z000”,
        “格式”:“ PDF”,
        “ media”:“ D”,
        “方向”:“ 0”,
        “ tempDin”:“ 12201511190845952490845SCHEDL2”​​,
        “ transType”:“ APPD”,
        “ operator”:“”,
        “ userkey2”:“ 0”,
        “ reason”:“计算机生成的文档”,
        “ archiveStatus”:“ A”,
        “ archiveDate”:“ 20151119”,
        “ deviceID”:“ 1”,
        “ packageID”:“ 1”,
        “ packageType”:“ 15007”,
        “ markedInd”:“ 0”,
        “ dspPageNumber”:“ 1”,
        “ aMedia”:“”,
        “ aDrive”:“”,
        “ description”:“输出”,
        “ folderName”:“ GAIL BARTLING”,
        “ docDescription”:“申请文件”,
        “ overlayID”:null,
        “ docIndex”:null
    }]
}

Response2(多个文件):
{
    “ irpage”:[{
        “抽屉”:“ HPPS”,
        “ folderNumber”:“ HPA00008047642”,
        “ docID”:“ 1”,
        “ pageNumber”:“ 1”,
        “ din”:“ HPPSHP​​A00008047642000001”,
        “ 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”,
        “批”:“ Z000”,
        “格式”:“ PDF”,
        “ media”:“ D”,
        “方向”:“ 0”,
        “ tempDin”:“ 12201511190845952490845SCHEDL2”​​,
        “ transType”:“ APPD”,
        “ operator”:“”,
        “ userkey2”:“ 0”,
        “ reason”:“计算机生成的文档”,
        “ archiveStatus”:“ A”,
        “ archiveDate”:“ 20151119”,
        “ deviceID”:“ 1”,
        “ packageID”:“ 1”,
        “ packageType”:“ 15007”,
        “ markedInd”:“ 0”,
        “ dspPageNumber”:“ 1”,
        “ aMedia”:“”,
        “ aDrive”:“”,
        “ description”:“输出”,
        “ folderName”:“ GAIL BARTLING”,
        “ docDescription”:“申请文件”,
        “ overlayID”:null,
        “ docIndex”:null
    },{
        “抽屉”:“ HPPS”,
        “ folderNumber”:“ HPA00008047642”,
        “ docID”:“ 4”,
        “ pageNumber”:“ 1”,
        “ din”:“ HPPSHP​​A00008047642000004”,
        “ 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”,
        “批”:“ Z000”,
        “格式”:“ PDF”,
        “ media”:“ D”,
        “方向”:“ 0”,
        “ tempDin”:“ 12201601050819308070819SCHEDL2”​​,
        “ transType”:“ FINA”,
        “ operator”:“”,
        “ userkey2”:“ 0”,
        “ reason”:“计算机生成的文档”,
        “ archiveStatus”:“ A”,
        “ archiveDate”:“ 20160105”,
        “ deviceID”:“ 1”,
        “ packageID”:“ 4”,
        “ packageType”:“ 15007”,
        “ markedInd”:“ 0”,
        “ dspPageNumber”:“ 1”,
        “ aMedia”:“”,
        “ aDrive”:“”,
        “ description”:“输出”,
        “ folderName”:“ GAIL BARTLING”,
        “ docDescription”:“最终取消通知”,
        “ overlayID”:null,
        “ docIndex”:null
    }]
}

当我提供Class targetClass作为
    新的HashMap >>()。getClass()
 Response2被解析,我在Map中得到结果,但是response1(Single Document)不起作用,并返回null。

或者如果我将Class targetClass作为新的HashMap >()。getClass()提供,则Response1会被解析,并且我会在Map中得到结果,但是response2(Multiple Documents)不会无法正常工作并返回null。

我不确定如何处理和解析服务的两个响应?

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();
}


任何帮助将不胜感激。

最佳答案

回应1(单一文件):-

{
 "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(多个文件)

{
 "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
  }
 ]
}


我会这样解决

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


我的课

public class MyClass {
    List<ArrayEntry> irpage;

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


使用您需要的修饰符(公共/私人)

Maven对Gson的依赖

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.4</version>
    </dependency>

10-07 13:21