SpringBoot整合swagger

一、在pom.xml文件中添加相关依赖

需要特别注意与springboot的版本关系,如下图所示,springboot版本为2.5.5,swagger版本为2.8.0

 

 

 

 二、在项目启动类同目录下,添加类Swagger2

package com.example.demotest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("自主学习API")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demotest")) //项目包路径,重要
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot整合Swagger")
.description("更多Spring Boot相关文章请关注:https://home.cnblogs.com/u/tff612/")
.termsOfServiceUrl("https://home.cnblogs.com/u/tff612/")
.contact("TFF")
.version("1.0")
.build();
}
}

三、在需要使用的controller方法上添加注解

 

 四、启动项目,访问http://localhost:8080/swagger-ui.html#/

 

posted @ 2021-10-16 21:10  ThisTFF  阅读(74)  评论(0)    收藏  举报