我们需要剔除掉spring Web模块中slf4j的日志部分,否则将会与slf4j2产生依赖冲突
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>${springboot.version}version>
<exclusions>
<exclusion>
<artifactId>logback-classicartifactId>
<groupId>ch.qos.logbackgroupId>
exclusion>
<exclusion>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-to-slf4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-log4j2artifactId>
<version>${springboot.version}version>
dependency>
<configuration status="WARN" monitorInterval="30">
<properties>
<property name="LOG_HOME">/opt/logsproperty>
<property name="LOG_NAME">nanjustar-server-logsproperty>
<property name="LOG_FORMAT">[%d{yyyy-MM-dd HH:mm:ss.SSS}] %p %t %c[%L] - %m %nproperty>
<property name="LOG_FORMAT_CONSOLE">%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level} [%t] %highlight{%c{1.}.%M(%L)}: %msg%n%throwableproperty>
<property name="BACKUP_HOME">${LOG_HOME}/$${date:yyyy-MM}property>
<property name="BACK_HZ">%d{yyyy-MM-dd}property>
properties>
<appenders>
<console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_FORMAT_CONSOLE}" disableAnsi="false" noConsoleNoAnsi="false"/>
console>
appenders>
<loggers>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="infoLog"/>
<appender-ref ref="warnLog"/>
<appender-ref ref="errorLog"/>
root>
loggers>
configuration>
/**
* @author 木字楠
* @description 项目主启动类
* @date 2022/8/11
*/
@Slf4j
@SpringBootApplication
public class MuZiNanApplication {
public static void main(String[] args) {
ConfigurableEnvironment env = SpringApplication.run(MuZiNanApplication.class, args).getEnvironment();
log.info("\n----------------------------------------------------------\n\t" +
"Application:'{}' is running! Access URLs:\n\t" +
"Local URL: \thttp://localhost:{}\n\t" +
"Document:\thttp://localhost:{}/doc.html\n" +
"----------------------------------------------------------",
env.getProperty("spring.application.name"), env.getProperty("server.port"), env.getProperty("server.port"));
}
}
<swagger.version>3.0.0swagger.version>
<knife4j.version>2.0.4knife4j.version>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-boot-starterartifactId>
<version>${swagger.version}version>
<exclusions>
<exclusion>
<groupId>io.swaggergroupId>
<artifactId>swagger-modelsartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.github.xiaoymingroupId>
<artifactId>knife4j-spring-boot-starterartifactId>
<version>${knife4j.version}version>
dependency>
# custom Configuration
application:
#Swagger Configuration
swagger:
isEnable: true
/**
* @author 木字楠
* @version 1.0
*/
@Configuration
public class SwaggerConfig {
@Value("${application.swagger.isEnable}")
private boolean isEnable;
@Bean
public Docket createSecurityRestApi(){
return createRestApi("system","space.muzinan.controller.security");
}
/**
* 创建api
* @param groupName 分组名称
* @param basePackage 包路径
* @return Docket
*/
public Docket createRestApi(String groupName,String basePackage) {
return new Docket(DocumentationType.OAS_30)
.enable(isEnable)
.apiInfo(apiInfo())
.groupName(groupName)
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.any())
.build();
}
/**
* api简介信息
* @return ApiInfo
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("标题:木字楠博客系统后台接口文档")
.description("描述:木字楠博客系统后端接口服务...")
.contact(new Contact("楠橘星", "www.muzinan.space", "[email protected]"))
.version("v1.0")
.build();
}
}
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.4.0version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.0version>
dependency>
/**
*
* MySQL 生成演示
*
*/
public static void main(String[] args) {
//1. 全局配置
GlobalConfig config = new GlobalConfig();
//是否支持AR模式
config.setActiveRecord(true)
.setAuthor("MuZiNan") //作者
.setOutputDir("D:\\Desktop\\Security") //生成路径
.setFileOverride(true)//是否文件覆盖,如果多次
.setServiceName("%sService") //设置生成的service接口名首字母是否为I
.setIdType(IdType.AUTO) //主键策略
.setBaseResultMap(true)
.setBaseColumnList(true)
.setDateType(DateType.ONLY_DATE)
.setSwagger2(true);
//2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
dsConfig.setDbType(DbType.MYSQL)
.setUrl("jdbc:mysql://[ip]:[port]/blog?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true")
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUsername("[用户名]")
.setPassword("[密码]");
//3.策略配置
StrategyConfig stConfig = new StrategyConfig();
stConfig.setCapitalMode(true) // 全局大写命名
.setNaming(NamingStrategy.underline_to_camel)// 数据库表映射到实体的命名策略
.setColumnNaming(NamingStrategy.underline_to_camel)
.setInclude("表名称") //生成的表
.setTablePrefix("表前缀")
.setControllerMappingHyphenStyle(true)
.setEntityLombokModel(true)
.setRestControllerStyle(true); // 表前缀
//4.包名策略
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("space.muzinan")//父包名
.setController("controller")
.setEntity("entity")
.setService("business")
.setMapper("mapper")
.setServiceImpl("business.impl");
//5.整合配置
AutoGenerator ag = new AutoGenerator().setGlobalConfig(config)
.setDataSource(dsConfig)
.setStrategy(stConfig)
.setPackageInfo(pkConfig);
ag.execute();
}
因为我们的项目是多模块项目,所以我们先暂且将所有代码生成在web模块。
生成完毕,我们手动将代码拖到对应模块
web模块 => 存放Controller
Business模块 => 主要存放接口实现类
Mapper模块 => Mapper接口 以及 xml文件
api 模块 => 实体类、api接口
common 模块 => 工具类、配置类、枚举类、常量类…
@Bean
public Docket createSecurityRestApi() {
return createRestApi("security", "space.muzinan.controller.security");
}
@Bean
public Docket createSystemRestApi() {
return createRestApi("system", "space.muzinan.controller.system");
}
@Bean
public Docket createBlogRestApi() {
return createRestApi("blog", "space.muzinan.controller.blog");
}
添加以上配制之后swagger被区分为三个分组,在Controller上添加@Api(tags ={“”})即可显示对应接口