SpringBoot Swagger简单使用

1.添加依赖pop.xml


            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        

2.新建Swagger配置类

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi()
    {
        Contact contact = new Contact("无聊的包子","abc.net","[email protected]");
        ApiInfo apiInfo = new ApiInfoBuilder().title("SpringBoot整合Swagger")
                .description("SpringBoot整合Swagger,详细信息......").version("9.0")
                .contact(contact)
                .license("The Apache License")
                .licenseUrl("www.baidu.com")
                .build();


        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swaggerpractice.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo);
    }
}

代码中的Contact 表示用户分别是用户名,用户主页,用户邮箱。
ApiInfo =》API信息、包括标题、详细信息、版本信息

3.运行访问

http://localhost:8080/swagger-ui.html
效果图
SpringBoot Swagger简单使用_第1张图片

这样就在SpringBoot中引入了Swagger,下篇文章Swagger显示API接口

你可能感兴趣的:(springBoot)