赞
踩
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
server:
port: 8080
knife4j:
enable: true
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); } }
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"; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。