Browse Source

修改空指针

master
王琳 5 months ago
parent
commit
f9e1af4f51
  1. 5
      pom.xml
  2. 7
      src/main/java/com/lh/controller/VoteController.java
  3. 2
      src/main/java/com/lh/exception/MyException.java
  4. 30
      src/main/java/com/lh/until/Utils.java

5
pom.xml

@ -69,6 +69,11 @@
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.25</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>

7
src/main/java/com/lh/controller/VoteController.java

@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
@CrossOrigin
@RestController
@ -29,12 +30,14 @@ public class VoteController {
}
//获取所有候选人
@PostMapping ("/getCandidates")
public RespBean getCandidates(@RequestBody String token) throws IOException {
public RespBean getCandidates(@RequestBody Map<String, String> query) throws IOException {
String token = query.get("token");
//System.out.println(token);
////将token的值分离出来
//int startIndex = token.indexOf('=') + 1; // 找到等号的位置并移动到等号后一位
//String tokenValue = token.substring(startIndex); // 提取等号后面的部分
token = "token=" + URLEncoder.encode(token.substring(10, token.length() - 2), "UTF-8");
// token = "token=" + URLEncoder.encode(token.substring(10, token.length() - 2), "UTF-8");
token = "token=" + URLEncoder.encode(token, "UTF-8");
System.out.println(token);
String voterJwcode = String.valueOf(new Utils().getJwcode(token));

2
src/main/java/com/lh/exception/MyException.java

@ -1,6 +1,6 @@
package com.lh.exception;
public class MyException extends Exception{
public class MyException extends RuntimeException {
public MyException(String msg) {
super(msg);
}

30
src/main/java/com/lh/until/Utils.java

@ -1,8 +1,12 @@
package com.lh.until;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lh.bean.RespBean;
import com.lh.bean.dto.TokenDTO;
import com.lh.exception.MyException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
@ -17,6 +21,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
@Slf4j
@Component
@ -40,21 +45,24 @@ public class Utils {
try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
int responseCode = response.getStatusLine().getStatusCode(); // 获取状态码
// 检查响应状态
if (responseCode == 200) {
if (responseCode != 200) {
throw new MyException("Failed : HTTP error code : " + responseCode);
}
// 读取响应体
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
// 使用Jackson解析JSON响应体
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonResponse = objectMapper.readTree(responseBody);
JsonNode dataNode = jsonResponse.get("data");
TokenDTO tokenDTO = new TokenDTO();
tokenDTO.setJwcode(dataNode.get("jwcode").asInt());
tokenDTO.setUsername(dataNode.get("username").asText());
return tokenDTO;
// 获取不到用户状态
if (StrUtil.isEmpty(responseBody)) {
throw new MyException("用户状态获取失败");
}
else {
throw new RuntimeException("Failed : HTTP error code : " + responseCode);
// 将token进行解析为RespBean
RespBean bean = JSONUtil.toBean(responseBody, RespBean.class);
if (bean.getCode() != 200) {
throw new MyException("用户状态获取失败");
}
return JSONUtil.toBean(bean.getData().toString(), TokenDTO.class);
}
}
}

Loading…
Cancel
Save