我有一个Rest模板交换获取服务。该服务返回响应如下:
Response1(单个文档): - { " irpage":[{ "抽屉":" 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", "司机":"", " dateCaptured":" 20151119", " timeCaptured":" 08:45:04", " singleInd":" S", " userID":" SCHEDL2", " status":" U", "批次":" Z000", "格式":" PDF", "媒体":" D", " orientation":" 0", " tempDin":" 12201511190845952490845SCHEDL2", " transType":" APPD", "运营商":"", " userkey2":" 0", "原因":"计算机生成文档", " archiveStatus":" A", " archiveDate":" 20151119", " deviceID":" 1", " packageID":" 1", " packageType":" 15007", "标记为":" 0", " dspPageNumber":" 1", " aMedia":"", " aDrive":"", "说明":"输出", " folderName":" GAIL BARTLING", " docDescription":"申请文件", " overlayID":null, " docIndex":null }] }
响应2(多个文档): { " irpage":[{ "抽屉":" 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", "司机":"", " dateCaptured":" 20151119", " timeCaptured":" 08:45:04", " singleInd":" S", " userID":" SCHEDL2", " status":" U", "批次":" Z000", "格式":" PDF", "媒体":" D", " orientation":" 0", " tempDin":" 12201511190845952490845SCHEDL2", " transType":" APPD", "运营商":"", " userkey2":" 0", "原因":"计算机生成文档", " archiveStatus":" A", " archiveDate":" 20151119", " deviceID":" 1", " packageID":" 1", " packageType":" 15007", "标记为":" 0", " dspPageNumber":" 1", " aMedia":"", " aDrive":"", "说明":"输出", " folderName":" GAIL BARTLING", " docDescription":"申请文件", " overlayID":null, " docIndex":null },{ "抽屉":" 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", "司机":"", " dateCaptured":" 20160105", " timeCaptured":" 08:19:30", " singleInd":" S", " userID":" SCHEDL2", " status":" U", "批次":" Z000", "格式":" PDF", "媒体":" D", " orientation":" 0", " tempDin":" 12201601050819308070819SCHEDL2", " transType":" FINA", "运营商":"", " userkey2":" 0", "原因":"计算机生成文档", " archiveStatus":" A", " archiveDate":" 20160105", " deviceID":" 1", " packageID":" 4", " packageType":" 15007", "标记为":" 0", " dspPageNumber":" 1", " aMedia":"", " aDrive":"", "说明":"输出", " folderName":" GAIL BARTLING", " docDescription":"取消的最终通知", " overlayID":null, " docIndex":null }] }
当我提供 Class< E> targetClass as 新的HashMap<字符串,列表<地图<字符串,字符串>>>()。getClass() 解析Response2并在Map中得到结果,但是response1(单个文档)不起作用并返回为null。
或者如果我提供类< E> targetClass as new HashMap<字符串,地图< String,String>>()。getClass()对Response1进行解析,我在Map中得到结果,但是response2(多个文档)不起作用并返回为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();
}
任何帮助将不胜感激。
答案 0 :(得分:2)
Response1(单个文档): -
{
"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
}
]
}
响应2(多个文档)
{
"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);
MyClass的
public class MyClass {
List<ArrayEntry> irpage;
public class ArrayEntry {
String drawer;
String folderNumber;
...
}
}
使用您需要的修饰符(公共/私人)
Gson的Maven依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>