SpringBoot整合swagger

一、swagger2官网

swagger2 官网:https://swagger.io/

二、maven 地址

springfox-swagger2:http://mvnrepository.com/artifact/io.springfox/springfox-swagger2

springfox-swagger-ui:http://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui

三、在 pom.xml 添加依赖

        
        
            io.springfox
            springfox-swagger2
            2.7.0
        

        
            io.springfox
            springfox-swagger-ui
            2.7.0
        

四、创建com.zhg.swagger.SwaggerConfig配置类

package com.zhg.demo.swagger;


import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")//enable
@EnableWebMvc
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhg.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("测试swagger")
                .description("RESTful APIs 接口文档集合")
                .termsOfServiceUrl("http://www.lemos.club/")
                .contact("zhg")//作者
                .version("1.0")
                //.license("The Apache License, Version 2.0")
                .build();
    }

}

五、在springboot启动类中添加注解@EnableSwagger2

package com.zhg.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class SbApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbApplication.class, args);
    }
}

六、测试

-输入http://localhost:9091/swagger-ui.html#/测试

swagger测试

七、注意

-在pom中不能缺少spring-boot-starter-web核心依赖

        
        
            org.springframework.boot
            spring-boot-starter-web
        

你可能感兴趣的:(SpringBoot整合swagger)