Browse Source

fix: 修复沃运维获取用户信息接口响应解析异常

weijianghai 2 years ago
parent
commit
1686fc33d8
1 changed files with 14 additions and 3 deletions
  1. 14 3
      src/main/java/com/nokia/service/WoyunweiService.java

+ 14 - 3
src/main/java/com/nokia/service/WoyunweiService.java

@@ -1,5 +1,6 @@
 package com.nokia.service;
 
+import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.nokia.vo.TokenFlagVo;
@@ -9,6 +10,8 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 import org.springframework.web.client.RestTemplate;
 
+import java.util.HashMap;
+
 @Slf4j
 @Service
 public class WoyunweiService {
@@ -18,11 +21,19 @@ public class WoyunweiService {
     public TokenFlagVo woyunweiReturn(String token) {
         String url = woyunweiUrl + token;
         RestTemplate restTemplate = new RestTemplate();
-        String r = restTemplate.postForObject(url, new Object(), String.class);
+        String r = restTemplate.postForObject(url, new HashMap<>(), String.class);
         log.info("沃运维获取用户信息响应: {}", r);
-        JsonObject jsonObject = JsonParser.parseString(r).getAsJsonObject();
-        String userName = jsonObject.get("userName").getAsString();
         TokenFlagVo vo = new TokenFlagVo();
+        vo.setIsValid(1);
+        if (!StringUtils.hasText(r)) {
+            return vo;
+        }
+        JsonObject jsonObject = JsonParser.parseString(r).getAsJsonObject();
+        JsonElement userNameElement = jsonObject.get("userName");
+        if (userNameElement == null) {
+            return vo;
+        }
+        String userName = userNameElement.getAsString();
         vo.setIsValid(StringUtils.hasText(userName) ? 0 : 1);
         vo.setLoginName(userName);
         return vo;