refactor: 替换net.sf.json为fastJson

This commit is contained in:
禾几海
2020-08-15 14:39:39 +08:00
parent 94c9fc1c46
commit 47e5030f52
3 changed files with 28 additions and 26 deletions

View File

@@ -2,10 +2,10 @@ package cn.celess.blog.service;
import cn.celess.blog.entity.model.PageData; import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.model.WebUpdateModel; import cn.celess.blog.entity.model.WebUpdateModel;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @author : xiaohai * @author : xiaohai
@@ -59,5 +59,5 @@ public interface WebUpdateInfoService {
* *
* @return * @return
*/ */
JSONObject getLastestUpdateTime(); Map<String, Object> getLastestUpdateTime();
} }

View File

@@ -10,20 +10,17 @@ import cn.celess.blog.service.WebUpdateInfoService;
import cn.celess.blog.util.DateFormatUtil; import cn.celess.blog.util.DateFormatUtil;
import cn.celess.blog.util.HttpUtil; import cn.celess.blog.util.HttpUtil;
import cn.celess.blog.util.ModalTrans; import cn.celess.blog.util.ModalTrans;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
/** /**
* @author : xiaohai * @author : xiaohai
@@ -84,24 +81,25 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
} }
@Override @Override
public JSONObject getLastestUpdateTime() { public Map<String, Object> getLastestUpdateTime() {
JSONObject jsonObject = new JSONObject(); Map<String, Object> map = new HashMap<>();
jsonObject.put("lastUpdateTime", DateFormatUtil.get(webUpdateInfoMapper.getLastestOne().getUpdateTime())); map.put("lastUpdateTime", DateFormatUtil.get(webUpdateInfoMapper.getLastestOne().getUpdateTime()));
jsonObject.put("lastUpdateInfo", webUpdateInfoMapper.getLastestOne().getUpdateInfo()); map.put("lastUpdateInfo", webUpdateInfoMapper.getLastestOne().getUpdateInfo());
try { try {
JSONArray array = JSONArray.fromObject(HttpUtil.get("https://api.github.com/repos/xiaohai2271/blog-frontEnd/commits?page=1&per_page=1")); ObjectMapper mapper = new ObjectMapper();
JSONObject object = array.getJSONObject(0); JsonNode root = mapper.readTree(HttpUtil.get("https://api.github.com/repos/xiaohai2271/blog-frontEnd/commits?page=1&per_page=1"));
JSONObject commit = object.getJSONObject("commit"); Iterator<JsonNode> elements = root.elements();
jsonObject.put("lastCommit", commit.getString("message")); JsonNode next = elements.next();
jsonObject.put("committerAuthor", commit.getJSONObject("committer").getString("name")); JsonNode commit = next.get("commit");
SimpleDateFormat sdf = new SimpleDateFormat(); map.put("lastCommit", commit.get("message"));
Instant parse = Instant.parse(commit.getJSONObject("committer").getString("date")); map.put("committerAuthor", commit.get("committer").get("name"));
jsonObject.put("committerDate", DateFormatUtil.get(Date.from(parse))); Instant parse = Instant.parse(commit.get("committer").get("date").asText());
jsonObject.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + object.getString("sha")); map.put("committerDate", DateFormatUtil.get(Date.from(parse)));
map.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + next.get("sha").asText());
} catch (IOException e) { } catch (IOException e) {
log.info("网络请求失败{}", e.getMessage()); log.info("网络请求失败{}", e.getMessage());
} }
return jsonObject; return map;
} }
private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) { private List<WebUpdateModel> list2List(List<WebUpdate> webUpdates) {

View File

@@ -3,7 +3,8 @@ package cn.celess.blog.util;
import cn.celess.blog.enmu.ResponseEnum; import cn.celess.blog.enmu.ResponseEnum;
import cn.celess.blog.entity.User; import cn.celess.blog.entity.User;
import cn.celess.blog.exception.MyException; import cn.celess.blog.exception.MyException;
import net.sf.json.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -31,24 +32,27 @@ public class RedisUserUtil {
return user; return user;
} }
@SneakyThrows
public User getWithOutExc() { public User getWithOutExc() {
String token = request.getHeader("Authorization"); String token = request.getHeader("Authorization");
if (token == null || token.isEmpty()) { if (token == null || token.isEmpty()) {
return null; return null;
} }
String email = jwtUtil.getUsernameFromToken(token); String email = jwtUtil.getUsernameFromToken(token);
return (User) JSONObject.toBean(JSONObject.fromObject(redisUtil.get(email + "-login")), User.class); return new ObjectMapper().readValue(redisUtil.get(email + "-login"), User.class);
} }
@SneakyThrows
public User set(User user) { public User set(User user) {
Long expire = redisUtil.getExpire(user.getEmail() + "-login"); Long expire = redisUtil.getExpire(user.getEmail() + "-login");
redisUtil.setEx(user.getEmail() + "-login", JSONObject.fromObject(user).toString(), redisUtil.setEx(user.getEmail() + "-login", new ObjectMapper().writeValueAsString(user),
expire > 0 ? expire : JwtUtil.EXPIRATION_SHORT_TIME, TimeUnit.MILLISECONDS); expire > 0 ? expire : JwtUtil.EXPIRATION_SHORT_TIME, TimeUnit.MILLISECONDS);
return user; return user;
} }
@SneakyThrows
public User set(User user, boolean isRemember) { public User set(User user, boolean isRemember) {
redisUtil.setEx(user.getEmail() + "-login", JSONObject.fromObject(user).toString(), redisUtil.setEx(user.getEmail() + "-login", new ObjectMapper().writeValueAsString(user),
isRemember ? JwtUtil.EXPIRATION_LONG_TIME : JwtUtil.EXPIRATION_SHORT_TIME, TimeUnit.MILLISECONDS); isRemember ? JwtUtil.EXPIRATION_LONG_TIME : JwtUtil.EXPIRATION_SHORT_TIME, TimeUnit.MILLISECONDS);
request.getSession().setAttribute("email", user.getEmail()); request.getSession().setAttribute("email", user.getEmail());
return user; return user;