日志记录自动记录接口响应数据
This commit is contained in:
parent
d20294ae98
commit
8927b3323f
@ -2,6 +2,7 @@ package cn.zhangdeman.context;
|
|||||||
|
|
||||||
import cn.zhangdeman.consts.RecordField;
|
import cn.zhangdeman.consts.RecordField;
|
||||||
import cn.zhangdeman.exception.CustomException;
|
import cn.zhangdeman.exception.CustomException;
|
||||||
|
import cn.zhangdeman.logger.LoggerInstance;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
@ -45,7 +46,11 @@ public class Response<Data> implements Serializable {
|
|||||||
@JsonProperty(RecordField.RESPONSE_COOKIE)
|
@JsonProperty(RecordField.RESPONSE_COOKIE)
|
||||||
private Map<String, String> cookie = new HashMap<>(); // 响应cookie
|
private Map<String, String> cookie = new HashMap<>(); // 响应cookie
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
|
private String contentType = "application/json;charset=utf-8";
|
||||||
|
@JsonIgnore
|
||||||
private RuntimeContext runtimeContext;
|
private RuntimeContext runtimeContext;
|
||||||
|
@JsonIgnore
|
||||||
|
private Boolean handleSuccess; // 是否处理成功响应
|
||||||
|
|
||||||
// 响应实例
|
// 响应实例
|
||||||
public Response(RuntimeContext runtimeContext) {
|
public Response(RuntimeContext runtimeContext) {
|
||||||
@ -90,48 +95,60 @@ public class Response<Data> implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 请求成功, 200 状态码
|
// 请求成功, 200 状态码
|
||||||
public Response<Data> success(Data data) {
|
public Response<Data> success(RuntimeContext runtimeContext, Data data) {
|
||||||
return success(HttpStatus.OK, data);
|
return success(runtimeContext, HttpStatus.OK, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求成功, 自定义http状态码
|
// 请求成功, 自定义http状态码
|
||||||
public Response<Data> success(HttpStatus httpStatus, Data data) {
|
public Response<Data> success(RuntimeContext runtimeContext, HttpStatus httpStatus, Data data) {
|
||||||
|
setHandleSuccess(true);
|
||||||
if (null == getCode()) {
|
if (null == getCode()) {
|
||||||
setCode("0");
|
setCode("0");
|
||||||
}
|
}
|
||||||
if (null == message) {
|
if (null == message) {
|
||||||
setMessage("请求成功");
|
setMessage("请求成功");
|
||||||
}
|
}
|
||||||
return any(httpStatus, getCode(), getMessage(), data);
|
return any(runtimeContext, httpStatus, getCode(), getMessage(), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求失败, 返回200
|
// 请求失败, 返回200
|
||||||
public Response<Data> failure(String code, String message, Object errorDetail) {
|
public Response<Data> failure(RuntimeContext runtimeContext, String code, String message, Object errorDetail) {
|
||||||
return failure(HttpStatus.OK, code, message, errorDetail);
|
return failure(runtimeContext, HttpStatus.OK, code, message, errorDetail);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response<Data> failure(HttpStatus httpStatus, String code, String message, Object errorDetail) {
|
public Response<Data> failure(RuntimeContext runtimeContext, HttpStatus httpStatus, String code, String message, Object errorDetail) {
|
||||||
|
setHandleSuccess(false);
|
||||||
setErrorDetail(errorDetail);
|
setErrorDetail(errorDetail);
|
||||||
return any(httpStatus, code, message, null);
|
return any(runtimeContext, httpStatus, code, message, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response<Data> failure(CustomException customException) {
|
public Response<Data> failure(RuntimeContext runtimeContext, CustomException customException) {
|
||||||
return failure(HttpStatus.OK, customException);
|
return failure(runtimeContext, HttpStatus.OK, customException);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Response<Data> failure(HttpStatus httpStatus, CustomException customException) {
|
public Response<Data> failure(RuntimeContext runtimeContext, HttpStatus httpStatus, CustomException customException) {
|
||||||
|
setHandleSuccess(false);
|
||||||
setErrorDetail(customException.getStackTrace()); // 异常堆栈
|
setErrorDetail(customException.getStackTrace()); // 异常堆栈
|
||||||
return any(httpStatus, customException.getCode(), customException.getMessage(), null);
|
return any(runtimeContext, httpStatus, customException.getCode(), customException.getMessage(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 任意姿势的响应
|
// 任意姿势的响应
|
||||||
public Response<Data> any(HttpStatus httpStatus, String code, String message, Data data) {
|
public Response<Data> any(RuntimeContext runtimeContext, HttpStatus httpStatus, String code, String message, Data data) {
|
||||||
|
this.runtimeContext = runtimeContext;
|
||||||
setHttpStatus(httpStatus);
|
setHttpStatus(httpStatus);
|
||||||
|
addHeader("Content-Type", getContentType()); // header追加响应类型
|
||||||
setHttpCode(httpStatus.value());
|
setHttpCode(httpStatus.value());
|
||||||
setCode(code);
|
setCode(code);
|
||||||
setMessage(message);
|
setMessage(message);
|
||||||
setData(data);
|
setData(data);
|
||||||
setResponseHeaderAndCookie();
|
setResponseHeaderAndCookie();
|
||||||
|
this.runtimeContext.setResponse((Response<Object>)this);
|
||||||
|
// 记录日志
|
||||||
|
if (getHandleSuccess()) {
|
||||||
|
LoggerInstance.REQUEST_OUTPUT_LOGGER.info("接口处理成功");
|
||||||
|
} else {
|
||||||
|
LoggerInstance.REQUEST_OUTPUT_LOGGER.error("接口处理失败");
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
// 序列化
|
// 序列化
|
||||||
|
@ -37,7 +37,8 @@ public class RuntimeContext implements Serializable {
|
|||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private HttpServletRequest httpServletRequest;// 请求实例
|
private HttpServletRequest httpServletRequest;// 请求实例
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private HttpServletResponse httpServletResponse; // 相应实例
|
private HttpServletResponse httpServletResponse; // 响应实例
|
||||||
|
|
||||||
|
|
||||||
// 序列化
|
// 序列化
|
||||||
@Override
|
@Override
|
||||||
|
@ -39,7 +39,7 @@ public class GlobalExceptionHandler {
|
|||||||
@ExceptionHandler(cn.zhangdeman.exception.CustomException.class)
|
@ExceptionHandler(cn.zhangdeman.exception.CustomException.class)
|
||||||
public ResponseEntity<Object> handleBusinessException(CustomException customException, HttpServletRequest httpServletRequest) {
|
public ResponseEntity<Object> handleBusinessException(CustomException customException, HttpServletRequest httpServletRequest) {
|
||||||
RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest);
|
RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest);
|
||||||
Response<Object> response = new Response<>(runtimeContext).failure(customException);
|
Response<Object> response = new Response<>(runtimeContext).failure(runtimeContext, customException);
|
||||||
return getResponseEntity(response);
|
return getResponseEntity(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,16 +62,23 @@ public class GlobalExceptionHandler {
|
|||||||
});
|
});
|
||||||
|
|
||||||
RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest);
|
RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest);
|
||||||
Response<Object> response = new Response<>(runtimeContext).failure("400", String.join(" - ", errors), errDetailList);
|
Response<Object> response = new Response<>(runtimeContext).failure(runtimeContext, "400", String.join(" - ", errors), errDetailList);
|
||||||
return getResponseEntity(response);
|
return getResponseEntity(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理没有此方法的异常
|
||||||
|
@ExceptionHandler(java.lang.NoSuchMethodError.class)
|
||||||
|
public ResponseEntity<Object> handleNoSuchMethodException(Exception ex, HttpServletRequest httpServletRequest) {
|
||||||
|
RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest);
|
||||||
|
Response<Object> response = new Response<>(runtimeContext).failure(runtimeContext,"500", ex.getMessage(), ex.getCause());
|
||||||
|
return getResponseEntity(response);
|
||||||
|
}
|
||||||
|
|
||||||
// 处理其他未捕获的异常
|
// 处理其他未捕获的异常
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
public ResponseEntity<Object> handleGlobalException(Exception ex, HttpServletRequest httpServletRequest) {
|
public ResponseEntity<Object> handleGlobalException(Exception ex, HttpServletRequest httpServletRequest) {
|
||||||
RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest);
|
RuntimeContext runtimeContext = getRuntimeContext(httpServletRequest);
|
||||||
Response<Object> response = new Response<>(runtimeContext).failure("500", ex.getMessage(), ex.getCause());
|
Response<Object> response = new Response<>(runtimeContext).failure(runtimeContext,"500", ex.getMessage(), ex.getCause());
|
||||||
return getResponseEntity(response);
|
return getResponseEntity(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user