Java常用包--knife4j

其他网址

Spring Boot 集成 Swagger-Bootstrap-UI,非常棒的解决方案

官网

官网文档:https://doc.xiaominfo.com/knife4j/documentation/
官网首页:https://doc.xiaominfo.com/

简介

        之前用过swagger,觉得不太好用,浏览技术网站时,偶然发现swagger-bootstrap-ui,能方便地将接口进行归类。
        早期,swagger-boostrap-ui是1.x版本,如今swagger-bootsrap-ui到2.x,同时也更改名字knife4j,适用场景从过去的单体到微服务。

实例

其他

官网文档:https://doc.xiaominfo.com/knife4j/documentation/get_start.html

依赖及配置

依赖

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

配置

package com.example.demo.config;

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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Knife4jConfig {
    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("我的标题")
                        .description("我的描述")
                        // .termsOfServiceUrl("http://www.xx.com/")
                        .contact(new Contact("daoren", "https://blog.csdn.net/feiying0canglang", "[email protected]"))
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("all")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}

代码

跟swagger写法一样,见:Java常用包--Swagger--基础_feiying0canglang的博客-CSDN博客

测试

knife4j跟Tomcat共用一个端口,默认Tomcat是8080,所以,这样访问即可:

http://localhost:8080/doc.html 

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/114487344