SpringBoot-Integrate Knife4j

Official document: https://doc.xiaominfo.com/knife4j/documentation/description.html

At the beginning, the original intention of the project was to write an enhanced version of the front-end UI of swagger, but with the development of the project, in the face of more and more personalized needs, the back-end Java code had to be written to meet the new needs, in swagger- Between 1.8.5 and 1.9.6 versions of bootstrap-ui, the back-end Java code and UI are mixed in a Jar package for developers to use. Although this method is very convenient for swagger integration , You only need to introduce the jar package, but it is a bit bloated under the microservice architecture.

Tried it, it's pretty good~~
Okay, add a launcher

 <!--knife4j-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.8</version>
        </dependency>

Write the configuration class again

@Configuration
@EnableSwagger2WebMvc // 更换注解
public class Knife4jConfiguration {
    
    

    @Bean
    public Docket createRestApi() {
    
    
        ApiInfo apiInfo = new ApiInfoBuilder()
                // 文档标题
                .title("Spring-boot项目")
                //文档描述
                .description("V2.0接口规范")
                //服务条款URL
                .termsOfServiceUrl("http://127.0.0.1:80/")
                //版本号
                .version("2.0.0")
                .build();

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                //是否开启
                .enable(true)
                .select()
                //设置basePackage会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.basePackage("com.knife4j.demo.controller"))
                //指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }


}

Then write a comment in the controller

@Controller
@Api(value = "页面接口",description = "跳转接口测试")
public class HelloController {
    
    
    @RequestMapping("/hello")
    @ApiImplicitParam(name = "url",value = "null",required = true)
    @ApiOperation(value = "跳转hello页面")
    public String index(){
    
    
        return "hello";
    }
}

Just in case, add an @EnableKnife4j annotation to the startup class
Startup... It
Insert picture description here
is still good for development, reducing the time to write development documents

Guess you like

Origin blog.csdn.net/qq_36008278/article/details/114367693