1.导入依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

2.创建配置类

@Configuration
@EnableSwagger2
public class SwaggerConf {
    @Bean
 public Docket petApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tao.swagger.controller")) //指定提供接口所在的基包
 .build();
    }
    /**
 * 该套 API 说明,包含作者、简介、版本、host、服务URL
 * @return
 */
 private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("demo api 说明")
                .contact(new Contact("allen","null","name@example.com"))
                .version("0.1")
                .termsOfServiceUrl("https://segmentfault.com")
                .description("demo api")
                .build();
    }
}

注意配置类上的@EnableSwagger2注解
3.编写测试controller

@Api("hello controller 接口测试")
@Controller
public class HelloController {
    @ApiOperation("hello 接口,返回hello success")
    @GetMapping("/hello")
    public String hello(){
        return "hello success";
    }
}

打开swagger页面
http://localhost:8080/swagger-ui/index.html
image.png


林慫慫
11 声望0 粉丝

引用和评论

0 条评论