高版本springboot整合swagger
第一次使用 高版本 springboot整合swagger存在一些问题,所以记录一下
这是我的springboot版本2.6: ![![Alt](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hdmF0YXIu](https://img-blog.csdnimg.cn/f3b9f7456e84432c9b339e69f9a32b9f.png)
导入maven依赖(如果是高版本的别随便整合依赖会有问题):
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
然后是swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* swagger docket实例
* @return
*/
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true) //是否激活swagger,如果是正式环境下,请把他设置为false
.groupName("LittleFox") //分组,可以设置多个docket
.select()
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("LittleFox")
.description("谷粒商城swagger")
.version("1.0")
.termsOfServiceUrl("termsOfServiceUrl")
.contact(new Contact("name","url","email"))
.build();
}
// 下面的是默认配置 apiInfo.default
// static {
// DEFAULT = new ApiInfo("Api Documentation", "Api Documentation", "1.0", "urn:tos", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
// }
}
然后在应用swagger模块的配置类中加上,这个一定得加,不然启动不了
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
在你应用swagger的模块主启动一定要加上@ComponentScan(“com.atguigu”),不然打开swagger什么都没有
@SpringBootApplication
@MapperScan("com.atguigu.gulimall.product.dao")
@ComponentScan("com.atguigu") //给swagger指定地点
@EnableDiscoveryClient
public class GulimallProductApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallProductApplication.class,args);
}
}
最后访问地址 http://localhost:10001/swagger-ui/ (端口号改成你自己的,其他不要变),就OK了