- 一、maven依赖
- 二、GlobalExceptionHandler.java
- 三、ResponseStandard.java
- 四、logback.xml
- 五、测试
GitHub: link. 欢迎star
一、maven依赖注意:本篇博客风格(不多比比就是撸代码!!!)
二、GlobalExceptionHandler.javaorg.projectlombok lombok1.18.22 provided
import com.andon.springbootutil.domain.ResponseStandard; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.servlet.http.HttpServletRequest; @Slf4j @RestControllerAdvice //对Controller增强,并返回json格式字符串 public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseStandard三、ResponseStandard.java
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @Builder @NoArgsConstructor @AllArgsConstructor public class ResponseStandard四、logback.xmlimplements Serializable { private int code; private String message; private T data; private int total; public static ResponseStandard successResponse(T t) { ResponseStandard response = new ResponseStandard<>(); response.setCode(200); response.setMessage("success!!"); response.setData(t); response.setTotal(0); return response; } public static ResponseStandard failureResponse(T t) { ResponseStandard response = new ResponseStandard<>(); response.setCode(-1); response.setMessage("failure!!"); response.setData(t); response.setTotal(0); return response; } }
五、测试%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n UTF-8 ./logs/info/log-stack.log trueINFO ACCEPT DENY ./logs/info/log-stack.%d{yyyy-MM-dd}.log 30 %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n UTF-8 ./logs/error/log-stack.log trueERROR ACCEPT DENY ./logs/error/log-stack.%d{yyyy-MM-dd}.log 30 %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n UTF-8 ./logs/debug/log-stack.log trueDEBUG ACCEPT DENY ./logs/debug/log-stack.%d{yyyy-MM-dd}.log 30 %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %L - %msg%n UTF-8
GitHub: link. 欢迎star
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)