Parcourir la source

fix: client无法解析list的返回值json

370774330@qq.com il y a 5 ans
Parent
commit
056f7a8db8

+ 37 - 42
seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabClient.java

@@ -50,7 +50,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
                 .build();
     }
 
-    private <T> T processResponse(ObjectMapper objectMapper, Response response, Class<T> clazz) throws SeecoderGitlabException {
+    private <T> T processResponse(ObjectMapper objectMapper, Response response, TypeReference<T> typeReference) throws SeecoderGitlabException {
         try {
             validateResponseStatus(response);
             String body = response.body().string();
@@ -62,28 +62,6 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
 //            if (code != 0) {
 //                throw new SeecoderGitlabException(BIZ_ERROR_CODE, msg);
 //            }
-            if (clazz == null) {
-                return null;
-            }
-            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;
             }
@@ -97,6 +75,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
         }
     }
 
+
     private void validateResponseStatus(Response response) throws SeecoderGitlabException {
         if (response.code() == 403) {
             throw SeecoderGitlabException.UNAHTORIZATION_ERROR;
@@ -125,7 +104,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            GitlabUserVO gitlabUserVO = processResponse(objectMapper, response, GitlabUserVO.class);
+            GitlabUserVO gitlabUserVO = processResponse(objectMapper, response, new TypeReference<GitlabUserVO>() {});
             return gitlabUserVO;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -152,7 +131,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, boolean.class);
+            return processResponse(objectMapper, response, new TypeReference<Boolean>() {});
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -174,7 +153,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            boolean isDeleted = processResponse(objectMapper, response, boolean.class);
+            boolean isDeleted = processResponse(objectMapper, response, new TypeReference<Boolean>() {
+            });
             return isDeleted;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -197,7 +177,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            GitlabGroup gitlabGroup = processResponse(objectMapper, response, GitlabGroup.class);
+            GitlabGroup gitlabGroup = processResponse(objectMapper, response, new TypeReference<GitlabGroup>() {
+            });
             return gitlabGroup;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -226,7 +207,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            boolean isAdded = processResponse(objectMapper, response, boolean.class);
+            boolean isAdded = processResponse(objectMapper, response, new TypeReference<Boolean>() {
+            });
             return isAdded;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -254,7 +236,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, ProjectVO.class);
+            return processResponse(objectMapper, response, new TypeReference<ProjectVO>() {
+            });
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -280,7 +263,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            projectId = processResponse(objectMapper, response, Integer.class);
+            projectId = processResponse(objectMapper, response, new TypeReference<Integer>() {
+            });
             return projectId;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -307,7 +291,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            projectId = processResponse(objectMapper, response, Integer.class);
+            projectId = processResponse(objectMapper, response, new TypeReference<Integer>() {
+            });
             return projectId;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -331,7 +316,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             final Call call = client.newCall(request);
             Response response = call.execute();
             Set<DiffVO> diffVOSet= new HashSet<>();
-            diffVOSet=processResponse(objectMapper, response, diffVOSet.getClass());
+            diffVOSet=processResponse(objectMapper, response, new TypeReference<Set<DiffVO>>() {
+            });
             return diffVOSet;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -361,7 +347,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, boolean.class);
+            return processResponse(objectMapper, response, new TypeReference<Boolean>() {
+            });
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -383,7 +370,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, boolean.class);
+            return processResponse(objectMapper, response, new TypeReference<Boolean>() {
+            });
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -409,7 +397,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, String.class);
+            return processResponse(objectMapper, response, new TypeReference<String>() {
+            });
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -435,7 +424,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, String.class);
+            return processResponse(objectMapper, response, new TypeReference<String>() {
+            });
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -469,7 +459,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, CommitVO.class);
+            return processResponse(objectMapper, response, new TypeReference<CommitVO>() {
+            });
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -492,7 +483,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             final Call call = client.newCall(request);
             Response response = call.execute();
             List<CommitVO> commitVOList=new ArrayList<>();
-            commitVOList=processResponse(objectMapper, response, commitVOList.getClass());
+            commitVOList=processResponse(objectMapper, response, new TypeReference<List<CommitVO>>() {
+            });
             return commitVOList;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -516,7 +508,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             final Call call = client.newCall(request);
             Response response = call.execute();
             List<BranchVO> branchVOList=new ArrayList<>();
-            branchVOList=processResponse(objectMapper, response, branchVOList.getClass());
+            branchVOList=processResponse(objectMapper, response, new TypeReference<List<BranchVO>>() {
+            });
             return branchVOList;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -545,7 +538,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            boolean isAdded = processResponse(objectMapper, response, boolean.class);
+            boolean isAdded = processResponse(objectMapper, response, new TypeReference<Boolean>() {
+            });
             return isAdded;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -569,7 +563,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             final Call call = client.newCall(request);
             Response response = call.execute();
             List<ProjectVO> projectVOList=new ArrayList<>();
-            projectVOList=processListResponse(objectMapper, response, new TypeReference<List<ProjectVO>>() {});
+            projectVOList=processResponse(objectMapper, response, new TypeReference<List<ProjectVO>>() {});
             return projectVOList;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
@@ -593,7 +587,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             final Call call = client.newCall(request);
             Response response = call.execute();
             List<Integer> userList=new ArrayList<>();
-            userList=processResponse(objectMapper, response, userList.getClass());
+            userList=processResponse(objectMapper, response, new TypeReference<List<Integer>>() {
+            });
             return userList;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);