Browse Source

fix: 加入一个转换list结果的方法

370774330@qq.com 5 years ago
parent
commit
e911143afc

+ 34 - 8
seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabClient.java

@@ -1,6 +1,8 @@
 package com.nju.edu.gitlab;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.nju.edu.gitlab.dto.*;
@@ -53,16 +55,40 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             validateResponseStatus(response);
             String body = response.body().string();
             JsonNode node = objectMapper.readTree(body);
-            Integer code = Integer.parseInt(node.get("code").asText());
-            String msg = node.get("msg").asText();
-            if (code != 0) {
-                throw new SeecoderGitlabException(BIZ_ERROR_CODE, msg);
-            }
+            //todo 注释的代码和ResourceResponse字段对应不上,我先改成能用的形式,后续在修改
+            // author: wph
+//            Integer code = Integer.parseInt(node.get("code").asText());
+//            String msg = node.get("msg").asText();
+//            if (code != 0) {
+//                throw new SeecoderGitlabException(BIZ_ERROR_CODE, msg);
+//            }
             if (clazz == null) {
                 return null;
             }
-            if (node.get("result")!=null)
-                return objectMapper.treeToValue(node.get("result"), clazz);
+            if (node.get("res")!=null)
+                //todo 这种写法list无法正常转换
+                return objectMapper.treeToValue(node.get("res"), clazz);
+            else
+                throw SeecoderGitlabException.NOT_FOUND_ERROR;
+        } catch (IOException e) {
+            logger.error("IOException", e);
+            throw SeecoderGitlabException.IO_ERROR;
+        }
+    }
+
+    /**
+     * todo 上种写法不支持list,但是改的话可能影响其他接口,故这里新写一个,测试后要和上一个方法合并
+     */
+    private <T> T processListResponse(ObjectMapper objectMapper, Response response, TypeReference<T> typeReference) throws SeecoderGitlabException {
+        try {
+            validateResponseStatus(response);
+            String body = response.body().string();
+            JsonNode node = objectMapper.readTree(body);
+            if (typeReference == null) {
+                return null;
+            }
+            if (node.get("res")!=null)
+                return objectMapper.readValue(node.get("res").traverse(), typeReference);
             else
                 throw SeecoderGitlabException.NOT_FOUND_ERROR;
         } catch (IOException e) {
@@ -543,7 +569,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             final Call call = client.newCall(request);
             Response response = call.execute();
             List<ProjectVO> projectVOList=new ArrayList<>();
-            projectVOList=processResponse(objectMapper, response, projectVOList.getClass());
+            projectVOList=processListResponse(objectMapper, response, new TypeReference<List<ProjectVO>>() {});
             return projectVOList;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);