Compare commits

...
9 Commits
Author SHA1 Message Date
wonder edbbcd64f4 🔄Update: 新增全局跨域配置 2025-10-18 14:43:21 +08:00
wonder 3939a22b24 🔄Update: 新增请求包装类 2025-10-18 14:40:24 +08:00
wonder eba78157d9 🔄Update: 新增全局异常处理器 2025-10-18 14:35:38 +08:00
wonder 6d870cb817 🔄Update: 增加响应包装类 2025-10-18 14:30:40 +08:00
wonder 4a451278af 🔄Update: 添加自定义异常 2025-10-18 14:22:07 +08:00
wonder 1383f48096 ✅Test: 增加 Controller 测试 2025-10-18 10:49:06 +08:00
wonder 287c797f4a ✅Test: 增加主启动类测试 2025-10-18 10:45:34 +08:00
wonder fc77a948db 🔧Chore: 添加工具类依赖 2025-10-18 10:41:07 +08:00
wonder d077350ceb 🔧Chore: 添加Spring Boot基础配置和依赖 2025-10-18 10:34:59 +08:00
16 changed files with 349 additions and 1 deletions
+1
View File
@@ -1 +1,2 @@
.idea/
target/
+53 -1
View File
@@ -13,8 +13,60 @@
<java.version>21</java.version>
</properties>
<dependencies>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
</parent>
<dependencies>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.9</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.14</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
@@ -0,0 +1,11 @@
package cn.hezhaohui.thumb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@@ -0,0 +1,32 @@
package cn.hezhaohui.thumb.common;
import cn.hezhaohui.thumb.exception.ErrorCode;
import lombok.Data;
import java.io.Serializable;
@Data
public class BaseResponse<T> implements Serializable {
private int code;
private T data;
private String message;
public BaseResponse(int code, T data, String message) {
this.code = code;
this.data = data;
this.message = message;
}
public BaseResponse(int code, T data) {
this(code, data, "");
}
public BaseResponse(ErrorCode errorCode) {
this(errorCode.getCode(), null, errorCode.getMessage());
}
}
@@ -0,0 +1,13 @@
package cn.hezhaohui.thumb.common;
import lombok.Data;
import java.io.Serializable;
@Data
public class DeleteRequest implements Serializable {
private Long id;
private static final long serialVersionUID = 1L;
}
@@ -0,0 +1,29 @@
package cn.hezhaohui.thumb.common;
import lombok.Data;
@Data
public class PageRequest {
/**
* 当前页号
*/
private int current = 1;
/**
* 页面大小
*/
private int pageize = 10;
/**
* 排序字段
*/
private String sortField;
/**
* 排序顺序(默认降序)
*/
private String sortOrder = "descend";
}
@@ -0,0 +1,22 @@
package cn.hezhaohui.thumb.common;
import cn.hezhaohui.thumb.exception.ErrorCode;
public class ResultUtils {
public static <T> BaseResponse<T> sucess(T data) {
return new BaseResponse<>(ErrorCode.SUCCESS.getCode(), data, ErrorCode.SUCCESS.getMessage());
}
public static BaseResponse<?> error(ErrorCode errorCode) {
return new BaseResponse<>(errorCode);
}
public static BaseResponse<?> error(int code, String message) {
return new BaseResponse<>(code, null, message);
}
public static BaseResponse<?> error(ErrorCode errorCode, String message) {
return new BaseResponse<>(errorCode.getCode(), null, message);
}
}
@@ -0,0 +1,19 @@
package cn.hezhaohui.thumb.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.exposedHeaders("*");
}
}
@@ -0,0 +1,15 @@
package cn.hezhaohui.thumb.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("health")
public class HealthController {
@GetMapping
public String health() {
return "Hello! This is my wonderland.";
}
}
@@ -0,0 +1,24 @@
package cn.hezhaohui.thumb.exception;
import lombok.Getter;
@Getter
public class BusinessException extends RuntimeException{
private final int code;
public BusinessException(int code, String message) {
super(message);
this.code = code;
}
public BusinessException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.code = errorCode.getCode();
}
public BusinessException(ErrorCode errorCode, String message) {
super(message);
this.code = errorCode.getCode();
}
}
@@ -0,0 +1,32 @@
package cn.hezhaohui.thumb.exception;
import lombok.Getter;
@Getter
public enum ErrorCode {
SUCCESS(0, "ok"),
PARAMS_ERROR(40000, "请求参数错误"),
NOT_LOGIN_ERROR(40100, "未登录"),
NO_AUTH_ERROR(40101, "无权限"),
NOT_FOUND_ERROR(40400, "请求数据不存在"),
FORBIDDEN_ERROR(40300, "禁止访问"),
SYSTEM_ERROR(50000, "系统内部异常"),
OPERATION_ERROR(50001, "操作失败");
/**
* 状态码
*/
private final int code;
/**
* 信息
*/
private final String message;
ErrorCode(int code, String message) {
this.code = code;
this.message = message;
}
}
@@ -0,0 +1,24 @@
package cn.hezhaohui.thumb.exception;
import cn.hezhaohui.thumb.common.BaseResponse;
import cn.hezhaohui.thumb.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(BusinessException.class)
public BaseResponse<?> businessExceptionHandler(BusinessException e) {
log.error("BusinessException", e);
return ResultUtils.error(e.getCode(), e.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
log.error("RuntimeException", e);
return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
}
}
@@ -0,0 +1,18 @@
package cn.hezhaohui.thumb.exception;
public class ThrowUtils {
public static void throwIf(boolean condition, RuntimeException runtimeException) {
if (condition) {
throw runtimeException;
}
}
public static void throwIf(boolean condition, ErrorCode errorCode) {
throwIf(condition, new BusinessException(errorCode));
}
public static void throwIf(boolean condition, ErrorCode errorCode, String message) {
throwIf(condition, new BusinessException(errorCode, message));
}
}
+35
View File
@@ -0,0 +1,35 @@
spring:
application:
name: thumb-up
mvc:
servlet:
path: /api
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://47.121.181.112:3306/thumb_up
username: root
password: dn293830
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# springdoc-openapi项目配置
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
api-docs:
path: /v3/api-docs
group-configs:
- group: 'default'
paths-to-match: '/**'
packages-to-scan: cn.hezhaohui.thumb.controller
# knife4j的增强配置,不需要增强可以不配
knife4j:
enable: true
setting:
language: zh_cn
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
@@ -0,0 +1,12 @@
package cn.hezhaohui.thumb;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ApplicationTest {
@Test
void contextLoads() {
}
}