From 4aee30eb684aeafa5641bc0401d9471e95cc6265 Mon Sep 17 00:00:00 2001 From: wol <1293433164@qq.com> Date: Sun, 22 Dec 2024 21:05:15 +0800 Subject: [PATCH] 008 --- weblog-springboot-008/pom.xml | 118 ++++++++++++++++++ .../weblog-module-admin/.gitignore | 33 +++++ .../weblog-module-admin/pom.xml | 38 ++++++ .../WeblogModuleAdminApplicationTests.java | 13 ++ .../weblog-module-common/.gitignore | 33 +++++ .../weblog-module-common/pom.xml | 49 ++++++++ .../weblog/common/aspect/ApiOperationLog.java | 17 +++ .../common/aspect/ApiOperationLogAspect.java | 102 +++++++++++++++ .../weblog/common/enums/ResponseCodeEnum.java | 31 +++++ .../exception/BaseExceptionInterface.java | 13 ++ .../weblog/common/exception/BizException.java | 24 ++++ .../exception/GlobalExceptionHandler.java | 85 +++++++++++++ .../weblog/common/package-info.java | 7 ++ .../weblog/common/utils/JsonUtil.java | 25 ++++ .../weblog/common/utils/Response.java | 77 ++++++++++++ .../WeblogModuleCommonApplicationTests.java | 13 ++ weblog-springboot-008/weblog-web/.gitignore | 33 +++++ weblog-springboot-008/weblog-web/pom.xml | 64 ++++++++++ .../weblog/web/WeblogWebApplication.java | 15 +++ .../weblog/web/controller/TestController.java | 33 +++++ .../com/quanxiaoha/weblog/web/model/User.java | 32 +++++ .../src/main/resources/application-dev.yml | 1 + .../src/main/resources/application-prod.yml | 5 + .../src/main/resources/application.yml | 10 ++ .../src/main/resources/logback-weblog.xml | 46 +++++++ .../weblog/web/WeblogWebApplicationTests.java | 26 ++++ 26 files changed, 943 insertions(+) create mode 100644 weblog-springboot-008/pom.xml create mode 100644 weblog-springboot-008/weblog-module-admin/.gitignore create mode 100644 weblog-springboot-008/weblog-module-admin/pom.xml create mode 100644 weblog-springboot-008/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java create mode 100644 weblog-springboot-008/weblog-module-common/.gitignore create mode 100644 weblog-springboot-008/weblog-module-common/pom.xml create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/enums/ResponseCodeEnum.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BaseExceptionInterface.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BizException.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/GlobalExceptionHandler.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/package-info.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/JsonUtil.java create mode 100644 weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/Response.java create mode 100644 weblog-springboot-008/weblog-module-common/src/test/java/com/quanxiaoha/weblog/common/WeblogModuleCommonApplicationTests.java create mode 100644 weblog-springboot-008/weblog-web/.gitignore create mode 100644 weblog-springboot-008/weblog-web/pom.xml create mode 100644 weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/WeblogWebApplication.java create mode 100644 weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/controller/TestController.java create mode 100644 weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/model/User.java create mode 100644 weblog-springboot-008/weblog-web/src/main/resources/application-dev.yml create mode 100644 weblog-springboot-008/weblog-web/src/main/resources/application-prod.yml create mode 100644 weblog-springboot-008/weblog-web/src/main/resources/application.yml create mode 100644 weblog-springboot-008/weblog-web/src/main/resources/logback-weblog.xml create mode 100644 weblog-springboot-008/weblog-web/src/test/java/com/quanxiaoha/weblog/web/WeblogWebApplicationTests.java diff --git a/weblog-springboot-008/pom.xml b/weblog-springboot-008/pom.xml new file mode 100644 index 0000000..a5bfef8 --- /dev/null +++ b/weblog-springboot-008/pom.xml @@ -0,0 +1,118 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + + 2.6.3 + + + + com.quanxiaoha + weblog-springboot + ${revision} + weblog-springboot + + 前后端分离博客 Weblog By 犬小哈 + + + pom + + + + + weblog-web + + weblog-module-admin + + weblog-module-common + + + + + + + 0.0.1-SNAPSHOT + 1.8 + UTF-8 + + ${java.version} + ${java.version} + + + 1.18.28 + 31.1-jre + 3.12.0 + 2.15.2 + + + + + + + com.quanxiaoha + weblog-module-admin + ${revision} + + + + com.quanxiaoha + weblog-module-common + ${revision} + + + + + com.google.guava + guava + ${guava.version} + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + + + + + aliyunmaven + aliyun + https://maven.aliyun.com/repository/public + + + diff --git a/weblog-springboot-008/weblog-module-admin/.gitignore b/weblog-springboot-008/weblog-module-admin/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/weblog-springboot-008/weblog-module-admin/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/weblog-springboot-008/weblog-module-admin/pom.xml b/weblog-springboot-008/weblog-module-admin/pom.xml new file mode 100644 index 0000000..8256d34 --- /dev/null +++ b/weblog-springboot-008/weblog-module-admin/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + + com.quanxiaoha + weblog-springboot + ${revision} + + + com.quanxiaoha + weblog-module-admin + weblog-module-admin + weblog-admin (负责管理后台相关功能) + + + + com.quanxiaoha + weblog-module-common + + + + + org.projectlombok + lombok + true + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + \ No newline at end of file diff --git a/weblog-springboot-008/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java b/weblog-springboot-008/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java new file mode 100644 index 0000000..dab9a4b --- /dev/null +++ b/weblog-springboot-008/weblog-module-admin/src/test/java/com/quanxiaoha/weblog/admin/WeblogModuleAdminApplicationTests.java @@ -0,0 +1,13 @@ +package com.quanxiaoha.weblog.admin; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class WeblogModuleAdminApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/weblog-springboot-008/weblog-module-common/.gitignore b/weblog-springboot-008/weblog-module-common/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/weblog-springboot-008/weblog-module-common/pom.xml b/weblog-springboot-008/weblog-module-common/pom.xml new file mode 100644 index 0000000..8c1cd90 --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + com.quanxiaoha + weblog-springboot + ${revision} + + + com.quanxiaoha + weblog-module-common + weblog-module-common + weblog-module-common (此模块用于存放一些通用的功能) + + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.springframework.boot + spring-boot-starter-aop + + + + + com.fasterxml.jackson.core + jackson-databind + + + + + diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java new file mode 100644 index 0000000..38eeb0f --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLog.java @@ -0,0 +1,17 @@ +package com.quanxiaoha.weblog.common.aspect; + +import java.lang.annotation.*; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +@Documented +public @interface ApiOperationLog { + /** + * API 功能描述 + * + * @return + */ + String description() default ""; + +} + diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java new file mode 100644 index 0000000..ef199b4 --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/aspect/ApiOperationLogAspect.java @@ -0,0 +1,102 @@ + +package com.quanxiaoha.weblog.common.aspect; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.quanxiaoha.weblog.common.utils.JsonUtil; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.*; +import org.aspectj.lang.reflect.MethodSignature; +import org.slf4j.MDC; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.UUID; +import java.util.function.Function; +import java.util.stream.Collectors; + +@Aspect +@Component +@Slf4j +public class ApiOperationLogAspect { + + /** 以自定义 @ApiOperationLog 注解为切点,凡是添加 @ApiOperationLog 的方法,都会执行环绕中的代码 */ + @Pointcut("@annotation(com.quanxiaoha.weblog.common.aspect.ApiOperationLog)") + public void apiOperationLog() {} + + /** + * 环绕 + * @param joinPoint + * @return + * @throws Throwable + */ + @Around("apiOperationLog()") + public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { + try { + // 请求开始时间 + long startTime = System.currentTimeMillis(); + + // MDC + MDC.put("traceId", UUID.randomUUID().toString()); + + // 获取被请求的类和方法 + String className = joinPoint.getTarget().getClass().getSimpleName(); + String methodName = joinPoint.getSignature().getName(); + + // 请求入参 + Object[] args = joinPoint.getArgs(); + // 入参转 JSON 字符串 + String argsJsonStr = Arrays.stream(args).map(toJsonStr()).collect(Collectors.joining(", ")); + + // 功能描述信息 + String description = getApiOperationLogDescription(joinPoint); + + // 打印请求相关参数 + log.info("====== 请求开始: [{}], 入参: {}, 请求类: {}, 请求方法: {} =================================== ", + description, argsJsonStr, className, methodName); + + // 执行切点方法 + Object result = joinPoint.proceed(); + + // 执行耗时 + long executionTime = System.currentTimeMillis() - startTime; + + // 打印出参等相关信息 + log.info("====== 请求结束: [{}], 耗时: {}ms, 出参: {} =================================== ", + description, executionTime, JsonUtil.toJsonString(result)); + + return result; + } finally { + MDC.clear(); + } + } + + /** + * 获取注解的描述信息 + * @param joinPoint + * @return + */ + private String getApiOperationLogDescription(ProceedingJoinPoint joinPoint) { + // 1. 从 ProceedingJoinPoint 获取 MethodSignature + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + + // 2. 使用 MethodSignature 获取当前被注解的 Method + Method method = signature.getMethod(); + + // 3. 从 Method 中提取 LogExecution 注解 + ApiOperationLog apiOperationLog = method.getAnnotation(ApiOperationLog.class); + + // 4. 从 LogExecution 注解中获取 description 属性 + return apiOperationLog.description(); + } + + /** + * 转 JSON 字符串 + * @return + */ + private Function toJsonStr() { + return arg -> JsonUtil.toJsonString(arg); + } + +} diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/enums/ResponseCodeEnum.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/enums/ResponseCodeEnum.java new file mode 100644 index 0000000..5963dda --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/enums/ResponseCodeEnum.java @@ -0,0 +1,31 @@ +package com.quanxiaoha.weblog.common.enums; + +import com.quanxiaoha.weblog.common.exception.BaseExceptionInterface; +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-15 10:33 + * @description: 响应异常码 + **/ +@Getter +@AllArgsConstructor +public enum ResponseCodeEnum implements BaseExceptionInterface { + + // ----------- 通用异常状态码 ----------- + SYSTEM_ERROR("10000", "出错啦,后台小哥正在努力修复中..."), + PARAM_NOT_VALID("10001", "参数错误"), + + + // ----------- 业务异常状态码 ----------- + PRODUCT_NOT_FOUND("20000", "该产品不存在(测试使用)"), + ; + + // 异常码 + private String errorCode; + // 错误信息 + private String errorMessage; + +} diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BaseExceptionInterface.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BaseExceptionInterface.java new file mode 100644 index 0000000..ad51d50 --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BaseExceptionInterface.java @@ -0,0 +1,13 @@ +package com.quanxiaoha.weblog.common.exception; + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-15 9:54 + * @description: 通用异常接口 + **/ +public interface BaseExceptionInterface { + String getErrorCode(); + + String getErrorMessage(); +} diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BizException.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BizException.java new file mode 100644 index 0000000..cf618c2 --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/BizException.java @@ -0,0 +1,24 @@ +package com.quanxiaoha.weblog.common.exception; + +import lombok.Getter; +import lombok.Setter; + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-15 9:52 + * @description: 业务异常 + **/ +@Getter +@Setter +public class BizException extends RuntimeException { + // 异常码 + private String errorCode; + // 错误信息 + private String errorMessage; + + public BizException(BaseExceptionInterface baseExceptionInterface) { + this.errorCode = baseExceptionInterface.getErrorCode(); + this.errorMessage = baseExceptionInterface.getErrorMessage(); + } +} diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/GlobalExceptionHandler.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..27f41bb --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/exception/GlobalExceptionHandler.java @@ -0,0 +1,85 @@ +package com.quanxiaoha.weblog.common.exception; + +import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum; +import com.quanxiaoha.weblog.common.utils.Response; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.bind.BindResult; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.http.HttpServletRequest; +import java.util.Optional; + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-15 10:14 + * @description: 全局异常处理 + **/ +@ControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + + /** + * 捕获自定义业务异常 + * @return + */ + @ExceptionHandler({ BizException.class }) + @ResponseBody + public Response handleBizException(HttpServletRequest request, BizException e) { + log.warn("{} request fail, errorCode: {}, errorMessage: {}", request.getRequestURI(), e.getErrorCode(), e.getErrorMessage()); + return Response.fail(e); + } + + /** + * 捕获参数校验异常 + * @return + */ + @ExceptionHandler({ MethodArgumentNotValidException.class }) + @ResponseBody + public Response handleMethodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e) { + // 参数错误异常码 + String errorCode = ResponseCodeEnum.PARAM_NOT_VALID.getErrorCode(); + + // 获取 BindingResult + BindingResult bindingResult = e.getBindingResult(); + + StringBuilder sb = new StringBuilder(); + + // 获取校验不通过的字段,并组合错误信息,格式为: email 邮箱格式不正确, 当前值: '123124qq.com'; + Optional.ofNullable(bindingResult.getFieldErrors()).ifPresent(errors -> { + errors.forEach(error -> + sb.append(error.getField()) + .append(" ") + .append(error.getDefaultMessage()) + .append(", 当前值: '") + .append(error.getRejectedValue()) + .append("'; ") + + ); + }); + + // 错误信息 + String errorMessage = sb.toString(); + + log.warn("{} request error, errorCode: {}, errorMessage: {}", request.getRequestURI(), errorCode, errorMessage); + + return Response.fail(errorCode, errorMessage); + } + + /** + * 其他类型异常 + * @param request + * @param e + * @return + */ + @ExceptionHandler({ Exception.class }) + @ResponseBody + public Response handleOtherException(HttpServletRequest request, Exception e) { + log.error("{} request error, ", request.getRequestURI(), e); + return Response.fail(ResponseCodeEnum.SYSTEM_ERROR); + } +} diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/package-info.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/package-info.java new file mode 100644 index 0000000..f36a31e --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/package-info.java @@ -0,0 +1,7 @@ +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-10 9:20 + * @description: TODO + **/ +package com.quanxiaoha.weblog.common; \ No newline at end of file diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/JsonUtil.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/JsonUtil.java new file mode 100644 index 0000000..17da53a --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/JsonUtil.java @@ -0,0 +1,25 @@ +package com.quanxiaoha.weblog.common.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-14 16:27 + * @description: JSON 工具类 + **/ +@Slf4j +public class JsonUtil { + + private static final ObjectMapper INSTANCE = new ObjectMapper(); + + public static String toJsonString(Object obj) { + try { + return INSTANCE.writeValueAsString(obj); + } catch (JsonProcessingException e) { + return obj.toString(); + } + } +} diff --git a/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/Response.java b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/Response.java new file mode 100644 index 0000000..dfa1743 --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/main/java/com/quanxiaoha/weblog/common/utils/Response.java @@ -0,0 +1,77 @@ +package com.quanxiaoha.weblog.common.utils; + +import com.quanxiaoha.weblog.common.exception.BaseExceptionInterface; +import com.quanxiaoha.weblog.common.exception.BizException; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-11 19:50 + * @description: 响应参数工具类 + **/ +@Data +public class Response implements Serializable { + + // 是否成功,默认为 true + private boolean success = true; + // 响应消息 + private String message; + // 异常码 + private String errorCode; + // 响应数据 + private T data; + + // =================================== 成功响应 =================================== + public static Response success() { + Response response = new Response<>(); + return response; + } + + public static Response success(T data) { + Response response = new Response<>(); + response.setData(data); + return response; + } + + // =================================== 失败响应 =================================== + public static Response fail() { + Response response = new Response<>(); + response.setSuccess(false); + return response; + } + + public static Response fail(String errorMessage) { + Response response = new Response<>(); + response.setSuccess(false); + response.setMessage(errorMessage); + return response; + } + + public static Response fail(String errorCode, String errorMessage) { + Response response = new Response<>(); + response.setSuccess(false); + response.setErrorCode(errorCode); + response.setMessage(errorMessage); + return response; + } + + public static Response fail(BizException bizException) { + Response response = new Response<>(); + response.setSuccess(false); + response.setErrorCode(bizException.getErrorCode()); + response.setMessage(bizException.getErrorMessage()); + return response; + } + + public static Response fail(BaseExceptionInterface baseExceptionInterface) { + Response response = new Response<>(); + response.setSuccess(false); + response.setErrorCode(baseExceptionInterface.getErrorCode()); + response.setMessage(baseExceptionInterface.getErrorMessage()); + return response; + } + +} diff --git a/weblog-springboot-008/weblog-module-common/src/test/java/com/quanxiaoha/weblog/common/WeblogModuleCommonApplicationTests.java b/weblog-springboot-008/weblog-module-common/src/test/java/com/quanxiaoha/weblog/common/WeblogModuleCommonApplicationTests.java new file mode 100644 index 0000000..8e92175 --- /dev/null +++ b/weblog-springboot-008/weblog-module-common/src/test/java/com/quanxiaoha/weblog/common/WeblogModuleCommonApplicationTests.java @@ -0,0 +1,13 @@ +package com.quanxiaoha.weblog.common; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class WeblogModuleCommonApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/weblog-springboot-008/weblog-web/.gitignore b/weblog-springboot-008/weblog-web/.gitignore new file mode 100644 index 0000000..549e00a --- /dev/null +++ b/weblog-springboot-008/weblog-web/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/weblog-springboot-008/weblog-web/pom.xml b/weblog-springboot-008/weblog-web/pom.xml new file mode 100644 index 0000000..1703433 --- /dev/null +++ b/weblog-springboot-008/weblog-web/pom.xml @@ -0,0 +1,64 @@ + + + 4.0.0 + + + com.quanxiaoha + weblog-springboot + ${revision} + + + com.quanxiaoha + weblog-web + weblog-web + weblog-web (入口项目,负责博客前台展示相关功能,打包也放在这个模块负责) + + + + com.quanxiaoha + weblog-module-common + + + + com.quanxiaoha + weblog-module-admin + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.springframework.boot + spring-boot-starter-validation + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/WeblogWebApplication.java b/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/WeblogWebApplication.java new file mode 100644 index 0000000..ff25423 --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/WeblogWebApplication.java @@ -0,0 +1,15 @@ +package com.quanxiaoha.weblog.web; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan({"com.quanxiaoha.weblog.*"}) // 多模块项目中,必需手动指定扫描 com.quanxiaoha.weblog 包下面的所有类 +public class WeblogWebApplication { + + public static void main(String[] args) { + SpringApplication.run(WeblogWebApplication.class, args); + } + +} diff --git a/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/controller/TestController.java b/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/controller/TestController.java new file mode 100644 index 0000000..811af6e --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/controller/TestController.java @@ -0,0 +1,33 @@ +package com.quanxiaoha.weblog.web.controller; + +import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum; +import com.quanxiaoha.weblog.common.exception.BizException; +import com.quanxiaoha.weblog.common.utils.Response; +import com.quanxiaoha.weblog.web.model.User; +import com.quanxiaoha.weblog.common.aspect.ApiOperationLog; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-10 10:34 + * @description: TODO + **/ +@RestController +@Slf4j +public class TestController { + + @PostMapping("/test") + @ApiOperationLog(description = "测试接口") + public Response test(@RequestBody @Validated User user) { + return Response.success(); + } + +} diff --git a/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/model/User.java b/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/model/User.java new file mode 100644 index 0000000..4d0b6da --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/main/java/com/quanxiaoha/weblog/web/model/User.java @@ -0,0 +1,32 @@ +package com.quanxiaoha.weblog.web.model; + +import lombok.Data; + +import javax.validation.constraints.*; + +/** + * @author: 犬小哈 + * @url: www.quanxiaoha.com + * @date: 2023-08-10 10:35 + * @description: TODO + **/ +@Data +public class User { + // 用户名 + @NotBlank(message = "用户名不能为空") // 注解确保用户名不为空 + private String username; + // 性别 + @NotNull(message = "性别不能为空") // 注解确保性别不为空 + private Integer sex; + + // 年龄 + @NotNull(message = "年龄不能为空") + @Min(value = 18, message = "年龄必须大于或等于 18") // 注解确保年龄大于等于 18 + @Max(value = 100, message = "年龄必须小于或等于 100") // 注解确保年龄小于等于 100 + private Integer age; + + // 邮箱 + @NotBlank(message = "邮箱不能为空") + @Email(message = "邮箱格式不正确") // 注解确保邮箱格式正确 + private String email; +} \ No newline at end of file diff --git a/weblog-springboot-008/weblog-web/src/main/resources/application-dev.yml b/weblog-springboot-008/weblog-web/src/main/resources/application-dev.yml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/main/resources/application-dev.yml @@ -0,0 +1 @@ + diff --git a/weblog-springboot-008/weblog-web/src/main/resources/application-prod.yml b/weblog-springboot-008/weblog-web/src/main/resources/application-prod.yml new file mode 100644 index 0000000..655a604 --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/main/resources/application-prod.yml @@ -0,0 +1,5 @@ +#================================================================= +# log 日志 +#================================================================= +logging: + config: classpath:logback-weblog.xml diff --git a/weblog-springboot-008/weblog-web/src/main/resources/application.yml b/weblog-springboot-008/weblog-web/src/main/resources/application.yml new file mode 100644 index 0000000..368487d --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/main/resources/application.yml @@ -0,0 +1,10 @@ +spring: + profiles: + # 默认激活 dev 环境 + active: dev + jackson: + # 设置后台返参,若字段值为 null, 则不返回 +# default-property-inclusion: non_null + # 设置日期字段格式 + date-format: yyyy-MM-dd HH:mm:ss + diff --git a/weblog-springboot-008/weblog-web/src/main/resources/logback-weblog.xml b/weblog-springboot-008/weblog-web/src/main/resources/logback-weblog.xml new file mode 100644 index 0000000..922dc0d --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/main/resources/logback-weblog.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + ${LOG_FILE}-%i.log + + 30 + + + 10MB + + + + + ${FILE_LOG_PATTERN} + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/weblog-springboot-008/weblog-web/src/test/java/com/quanxiaoha/weblog/web/WeblogWebApplicationTests.java b/weblog-springboot-008/weblog-web/src/test/java/com/quanxiaoha/weblog/web/WeblogWebApplicationTests.java new file mode 100644 index 0000000..d2b347b --- /dev/null +++ b/weblog-springboot-008/weblog-web/src/test/java/com/quanxiaoha/weblog/web/WeblogWebApplicationTests.java @@ -0,0 +1,26 @@ +package com.quanxiaoha.weblog.web; + +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +@Slf4j +class WeblogWebApplicationTests { + + @Test + void contextLoads() { + } + + @Test + void testLog() { + log.info("这是一行 Info 级别日志"); + log.warn("这是一行 Warn 级别日志"); + log.error("这是一行 Error 级别日志"); + + // 占位符 + String author = "犬小哈"; + log.info("这是一行带有占位符日志,作者:{}", author); + } + +}