SpringBoot 整合 Knife4j

1. pom.xml

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-webartifactId>
	<version>2.6.3version>
dependency>
<dependency>
	<groupId>com.github.xiaoymingroupId>
	<artifactId>knife4j-openapi2-spring-boot-starterartifactId>
	<version>4.2.0version>
dependency>

2. application.yml

server:
  port: 8080

knife4j:
  enable: true

3. 创建配置类

SwaggerConfig

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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.EnableSwagger2WebMvc;

import java.net.Inet4Address;
import java.net.InetAddress;

/**
 * Swagger 配置
 */
@Configuration
@EnableKnife4j
@EnableSwagger2WebMvc
public class SwaggerConfig implements WebMvcConfigurer, CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(SwaggerConfig.class);

    @Value("${server.port}")
    private Integer port;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("API 接口文档")
                        .contact(new Contact("cnbai", "https://blog.csdn.net/baihaibo1024", ""))
                        .version("1.0")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 拦截器过滤
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 项目启动打印 swagger 路径
     */
    @Override
    public void run(String... args) throws Exception {
        final InetAddress localHost = Inet4Address.getLocalHost();
        String path = "http://" + localHost.getHostAddress() + ":" + port + "/doc.html";
        logger.info("Swagger-UI: {}", path);
    }
}

4. 创建测试类

TestController

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试 Swagger
 */
@Api(tags = "测试")
@RestController
public class TestController {

    @ApiOperation(value = "测试 Swagger", notes = "测试 Swagger")
    @GetMapping(path = "/test")
    public String test() {
        return "测试 Swagger";
    }
}

5. 测试 Swagger 地址

http://localhost:8080/doc.html

你可能感兴趣的:(SpringBoot,spring,boot,后端,java)