spring boot: build a project from scratch - day 4 console output log beautification + swagger2


My brain hurts, I have taken a break, but I always feel that blogging is a bit addictive, so let’s get something else

1. Logback.xml configuration log beautification

Many configurations can be made in logback.xml, but the most important thing is <append>the content under the label. Combining <property>the label can realize the beautification of the console output statement. for example:
insert image description here

<!--定义日志文件的输出样式-->
<property name="beautyPattern" value="%d{yyyy-MM-dd HH:mm:ss} | %highlight(%-5level) | %boldYellow(%thread) | %boldGreen(%logger) | %msg%n"/>

<!--输出到控制台-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>${beautyPattern}</pattern>
    </encoder>
</appender>

2. Integrate swagger2

In fact, I feel that I don’t need to use swagger to play alone, but 【Go to him!
Direct reference to the article: https://www.jianshu.com/p/583e302caa83

1. Introduce dependencies
<!--swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>
2. Write configuration files
package com.dingx.personal.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    
    
    //是否开启swagger,正式环境一般是需要关闭的,可根据springboot的多环境配置进行设置
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    //自己的包名
    private static final String basePackage = "com.dingx.personal";

    @Bean
    public Docket createRestApi() {
    
    
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled).select()
                // 扫描的路径包
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

    private ApiInfo apiInfo() {
    
    
        return new ApiInfoBuilder()
                .title("SpringBoot-Swagger2集成 : dingx")
                //描述信息
                .description("dingx")
                .version("1.0.0")
                .build();
    }
}

3. Add swagger annotations everywhere

The controller
insert image description here
entity
insert image description here
runs the project
...
...
...
See here, congratulations, the article has pitfalls =. =
When the brain hurts, seeing the pit is about to explode.
Run it directly and report an error: org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'xmlModelPlugin'
needs to introduce jaxb-api into the pom . It is said that java9 and above are all built-in. java8 I'm really sorry

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>

According to the rumors on the Internet, the introduced version is 2.3.0, but if you can't live or die, just let it go, whichever version you can get is whichever version you want, and you should follow the fate.

Run the project, add /swagger-ui.html after the address
insert image description here
PS: here is a small function of Mybatis plus

#全局逻辑删除字段值 3.3.0开始支持,详情看下面。
mybatis-plus.global-config.db-config.logic-delete-field=removed 
# 逻辑已删除值(默认为 1)
mybatis-plus.global-config.db-config.logic-delete-value=1 
# 逻辑未删除值(默认为 0)
mybatis-plus.global-config.db-config.logic-not-delete-value=0 

After specifying the tombstone field value here, there is no need to add @TableLogictags in the entity class.

In the next article, try the automatic code generation of Mybatis plus, handwriting entity+service+dao+mapper is a bit troublesome =. =

Guess you like

Origin blog.csdn.net/qq_16253859/article/details/106107579