This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>com.tiesheng.springboot-parent</groupId>
|
||||
<artifactId>springboot-parent</artifactId>
|
||||
<version>2.0.20</version>
|
||||
<version>2.0.21</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>springboot-web</artifactId>
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.tiesheng.web.service;
|
||||
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.tiesheng.web.mapper.CoreLogApiMapper;
|
||||
import com.tiesheng.web.pojos.dao.CoreLogApi;
|
||||
@@ -12,6 +11,8 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
@Component
|
||||
public class OkHttpLogInterceptor implements Interceptor {
|
||||
@@ -22,11 +23,14 @@ public class OkHttpLogInterceptor implements Interceptor {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Response intercept(@NotNull Chain chain) {
|
||||
|
||||
CoreLogApi logApi = new CoreLogApi();
|
||||
public Response intercept(@NotNull Chain chain) throws IOException {
|
||||
|
||||
Request request = chain.request();
|
||||
if (Objects.equals(request.header("skipApiLog"), "true")) {
|
||||
return chain.proceed(request);
|
||||
}
|
||||
|
||||
CoreLogApi logApi = new CoreLogApi();
|
||||
logApi.setUrl(request.url().toString());
|
||||
logApi.setContent(requestBody2String(request));
|
||||
|
||||
@@ -47,10 +51,29 @@ public class OkHttpLogInterceptor implements Interceptor {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("code", -1);
|
||||
object.put("message", JSON.toJSONString(e));
|
||||
object.put("code", 500);
|
||||
object.put("message", e.getMessage());
|
||||
object.put("exception", e);
|
||||
logApi.setRespBody(object.toJSONString());
|
||||
|
||||
response = new Response.Builder()
|
||||
|
||||
// 必须设置请求对象
|
||||
.request(chain.request())
|
||||
|
||||
// 指定协议版本
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
|
||||
// 设置HTTP状态码
|
||||
.code(object.getInteger("code"))
|
||||
|
||||
// 状态信息
|
||||
.message(object.getString("message"))
|
||||
|
||||
// 设置响应体
|
||||
.body(ResponseBody.create(MediaType.parse("application/json"), object.toJSONString()))
|
||||
.headers(new Headers.Builder().build()) // 可选:设置响应头
|
||||
.build();
|
||||
}
|
||||
|
||||
ThreadUtil.execute(() -> coreLogApiMapper.insert(logApi));
|
||||
|
||||
Reference in New Issue
Block a user