根据上一步的操作创建了三个子模块,分别是:
- demo-admin: 程序入口
- demo-user:用户管理模块
- demo-common: 公共模块,所有模块都依赖
配置日志logback:
logback是springboot的内置日志框架,所以比较方便;
直接在resources文件夹下新建logback.xml文件既可
logback.xml是放到了demo-admin模块中,因为此模块是程序入口,方便日志的统一;
在demo-admin模块的pom文件中添加依赖:
主要依赖了springboot的web和其他模块
代码语言:html复制 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.exp</groupId>
<artifactId>demo-common</artifactId>
</dependency>
<dependency>
<groupId>com.exp</groupId>
<artifactId>demo-user</artifactId>
</dependency>
logback.xml文件配置:
代码语言:html复制<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="60 seconds" debug="false">
<property name="LOG_PATH" value="logs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/debug.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_PATH}/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="FILE-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/error.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_PATH}/error.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- Send logs to both console and file audit -->
<logger name="com.exp" level="debug" additivity="false">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="FILE-ERROR" />
</root>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE-AUDIT" />
<!-- <appender-ref ref="FILE-ERROR" />-->
</root>
</configuration>
编码启动类:
代码语言:java复制import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
@SpringBootConfiguration
@EnableAutoConfiguration
@ImportAutoConfiguration(classes = {
},exclude = {
// MongoDataAutoConfiguration.class,
MongoAutoConfiguration.class
})
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class,args);
}
}
在idea中启动程序,可以再模块的同级目录中生成logs文件夹
引入swagger3:
swagger是一个接口管理工具,springboot集成swagger可以生成接口管理页面,方便调试;
- 第一步:创建接口;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/server")
public class ServerController {
@GetMapping(value = "/test")
public WebResult<String> test(){
return WebResult.build("it is work!");
}
}
此处创建了一个GET接口,测试接口服务是否正常;
WebResult是一个返回值包装类,用于同一返回值;
- 第二步:引入swagger3的依赖:
关于swagger3:
目前大部分使用的是swagger2.但是2已经停止维护了。swagger3是swagger2的升级版,更易于与springboot集成;
此次依赖的引入放到了demo-common模块中,方便后续其他模块依赖common接可以直接使用swagger;
在demo-common的pom文件中添加依赖:
代码语言:html复制 <dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.4.3</version>
</dependency>
在java配置代码中添加OpenApi:
代码语言:java复制 @Bean
public OpenAPI openAPI() {
return new OpenAPI()
.info(new io.swagger.v3.oas.models.info.Info()
.title("demo API")
.description("接口后台")
.termsOfService("http://www.demo.com")
.contact(new io.swagger.v3.oas.models.info.Contact()
.name("xxxx有限公司")
.url("http://www.demo.com")
.email("service@demo.com")
)
.license(new io.swagger.v3.oas.models.info.License()
.name("XXX all rights reserved")
.url("http://www.demo.com")
)
.version(getVersion(getClass()))
);
}
在demo-admin中运行启动类;
访问
http://127.0.0.1:port/swagger-ui
swagger3基本不需要额外的配置;
如果不想启用swagger3,可添加配置
#swagger配置 默认true
springdoc.api-docs.enabled=false