1. jackson配置类

jackson配置类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

/**
* @Classname JacksonConfig
* @Description Jackson配置类
* @Date 2024/7/26 上午10:12
* @Created by brokenarr0w
*/
@Configuration
@Slf4j
public class JacksonConfig {
@Bean
@ConditionalOnMissingBean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
log.info("[Jackson2ObjectMapperBuilderCustomizer][初始化customizer配置]");
return builder -> {
builder.locale(Locale.CHINA);
builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
builder.serializerByType(Long.class, ToStringSerializer.instance);
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DatePattern.NORM_DATETIME_FORMATTER));
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DatePattern.NORM_DATETIME_FORMATTER));
};
}
}

2. 跨域配置

跨域配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
* @Classname WebConfig
* @Description TODO
* @Date 2024/7/26 上午10:10
* @Created by brokenarr0w
*/
@Configuration
@Slf4j
public class WebConfig {

/**
* 跨域配置
*/
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// 设置访问源地址
config.addAllowedOriginPattern("*");
// 设置访问源请求头
config.addAllowedHeader("*");
// 设置访问源请求方法
config.addAllowedMethod("*");
// 有效期 1800秒
config.setMaxAge(1800L);
// 添加映射路径,拦截一切请求
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
// 返回新的CorsFilter
return new CorsFilter(source);
}

}

3. 基础实体类

基础实体类

这里是基础结果类和一些枚举

3.1Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import com.brokenarrow.framework.common.exception.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

/**
* @Classname BaseResponse
* @Description TODO
* @Date 2024/7/26 上午8:36
* @Created by brokenarr0w
*/
@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class Result<T> {
private int code;
private String message;
private T data;
private final static int SUCCESS_CODE = 200;
private final static String SUCCESS_MSG = "请求成功";

public static <T> Result<T> success(T data) {
return new Result<>(SUCCESS_CODE,SUCCESS_MSG, data);
}
public static <T> Result<T> success() {
return success(null);
}
public static <T> Result<T> error(ErrorCode responseEnum) {
return new Result<>(responseEnum.getCode(), responseEnum.getMsg(), null);
}

public static Result<String> error(int code, String msg) {
return new Result<>(code,msg,null);
}
}

ErrorCode类需和Result类配合使用

3.2 ErrorCode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;


/**
* @Classname ErrorCode
* @Description 错误码枚举类
* @Date 2024/7/5 下午4:46
* @Created by brokenarr0w
*/

@AllArgsConstructor
@NoArgsConstructor
@Getter
public enum ErrorCode {
UNAUTHORIZED(401, "还未授权,不能访问"),
FORBIDDEN(403, "没有权限,禁止访问"),
REFRESH_TOKEN_INVALID(400, "token 已失效"),
INTERNAL_SERVER_ERROR(500, "服务器异常,请稍后再试"),
BAD_REQUEST(400, "请求参数错误"),
USER_NOT_EXIST(404, "用户名或密码错误"),
HAS_ARTICLE_BY_TAG(500,"该标签下已有文章,无法删除"),
HAS_ARTICLE_BY_CATEGORY(500,"该分类下已有文章,无法删除"),
USER_PASSWORD_ERROR(400,"密码错误");
private int code;
private String msg;
}

3.3 PageResult

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
* @author wnhyang
*/
@Data
public final class PageResult<T> implements Serializable {

@Serial
private static final long serialVersionUID = -3938601703372465470L;

private List<T> list;

private Long total;

public PageResult() {
}

public PageResult(List<T> list, Long total) {
this.list = list;
this.total = total;
}

public PageResult(Long total) {
this.list = new ArrayList<>();
this.total = total;
}

public static <T> PageResult<T> empty() {
return new PageResult<>(0L);
}

public static <T> PageResult<T> empty(Long total) {
return new PageResult<>(total);
}
}

3.ExcelUtils

以下代码配合easyexcel使用

表格导入导出工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import com.alibaba.excel.EasyExcel;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
* @author brokenarr0w
* @date 2024/5/14
**/
public class ExcelUtils {

/**
* 从Excel文件中读取数据。
*
* @param file 需要读取的MultipartFile文件对象,通常来自上传的文件。
* @param head 数据实体的Class类型,用于解析Excel的头部信息并映射到对应的实体对象。
* @return 返回一个包含读取到的所有数据实体的List集合。
* @throws IOException 当读取文件发生错误时抛出IOException。
*/
public static <T> List<T> read(MultipartFile file, Class<T> head) throws IOException {
// 使用EasyExcel框架读取Excel文件内容
return EasyExcel.read(file.getInputStream(), head, null)
.autoCloseStream(Boolean.FALSE)
.doReadAllSync();
}

/**
* 将数据写入Excel文件。
*
* @param response HttpServletResponse对象,用于将Excel文件写入响应。
* @param fileName Excel文件的名称,用于设置响应的Content-Disposition头信息。
* @param sheetName Excel文件的sheet名称,用于设置Excel文件的sheet名称。
* @param head 数据实体的Class类型,用于解析Excel的头部信息并映射到对应的实体对象。
* @param data 需要写入的数据,类型为List<T>,其中T为数据实体的Class类型。
*/
public static <T> void write(HttpServletResponse response, String fileName, String sheetName, Class<T> head, List<T> data) throws IOException {
// 输出 Excel
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
EasyExcel.write(response.getOutputStream(), head)
.autoCloseStream(Boolean.FALSE)
.sheet(sheetName).doWrite(data);
}
}

4.DateUtils

日期工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @Classname DateUtils
* @Description 日期解析工具
* @Date 2024/7/30 下午9:18
* @Created by brokenarr0w
*/

public class DateUtils {
/** 时间格式(yyyy-MM-dd) */
public final static String DATE_PATTERN = "yyyy-MM-dd";
/** 时间格式(yyyy-MM-dd HH:mm:ss) */
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

/**
* 日期格式化 日期格式为:yyyy-MM-dd
* @param date 日期
* @return 返回yyyy-MM-dd格式日期
*/
public static String format(Date date) {
return format(date, DATE_PATTERN);
}

/**
* 日期格式化 日期格式为:yyyy-MM-dd
* @param date 日期
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
* @return 返回yyyy-MM-dd格式日期
*/
public static String format(Date date, String pattern) {
if(date != null){
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
return null;
}

/**
* 日期解析
* @param date 日期
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN
* @return 返回Date
*/
public static Date parse(String date, String pattern) {
try {
return new SimpleDateFormat(pattern).parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}