一、基础配置类
1、FastJson
1.1 在pom.xml文件中引入FastJson依赖:
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.9</version>
</dependency>
1.2 FastJson配置
@Bean//使用@Bean注入fastJsonHttpMessageConvert
public HttpMessageConverter fastJsonHttpMessageConverters() {
// 创建FastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 配置FastJson
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
SerializeConfig.globalInstance.put(Long.class, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(SerializeConfig.globalInstance);
fastConverter.setFastJsonConfig(fastJsonConfig);
// 添加到消息转换器列表中
HttpMessageConverter<?> converter = fastConverter;
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(fastJsonHttpMessageConverters());
}
1.3 在Spring Boot启动类上添加@EnableWebMvc注解:
@SpringBootApplication
@EnableWebMvc
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
2、
二、通用工具类
-
常用工具类:使用常用的工具类可以方便地实现一些常用的功能,如字符串处理、日期处理等,提高代码复用性和可读性。
-
多环境配置文件:使用多环境配置文件可以方便地在不同的环境中使用不同的配置,如数据库连接信息、Redis 配置等。
-
Maven 多环境配置:使用 Maven 的 Profile 功能可以方便地在不同的环境中使用不同的配置文件,避免手工修改配置文件的问题。
-
日志配置:使用日志配置可以方便地记录系统运行日志,提高系统可维护性和可读性。
1、【基础配置类】:例如BaseConfig、BaseMapper、BaseService等,提供一些基础的配置和方法,如分页查询、批量插入、更新、删除等。
2、【通用工具类】:例如日期格式化工具、加密解密工具、字符串处理工具等,提供一些通用的方法,可以在项目中多次使用,避免重复编写。
3、【全局异常处理类】:例如GlobalExceptionHandler,用于捕获全局异常信息并进行统一处理,防止异常信息泄露,提高代码的健壮性和安全性。
4、【日志处理类】:例如LogAspect,用于记录日志信息,可以方便地统计和分析应用程序的运行情况。
5、【常量类】:例如Constant,用于定义项目常量,避免在代码中出现魔法数字和魔法字符串,提高代码的可读性和可维护性。
6、【统一返回结果类】:例如Result,用于封装API接口的返回结果,规范接口返回格式,避免在不同接口返回结果格式不一致的问题。
7、【配置文件】:例如application.properties和application.yml,用于配置应用程序的一些参数、端口号、数据库连接等信息,方便管理和维护。
8、【测试类】:例如单元测试和集成测试,用于测试应用程序的各个模块是否正常工作,提高代码的可靠性和稳定性。