SpringBoot搭建API文档插件Swagger并美化

SpringBoot搭建API文档插件Swagger并美化

博客说明

文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!

地址

swagger-bootstrap-ui官网

导入依赖



    io.springfox
    springfox-swagger2
    2.9.2




    io.springfox
    springfox-swagger-ui
    2.9.2




    com.github.xiaoymin
    swagger-bootstrap-ui
    1.9.3

创建配置文件

SpringBoot搭建API文档插件Swagger并美化_第1张图片

SwaggerConfig
package com.guizimo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.guizimo.controller")) //此次每次使用须换成自己的web接口的全限定类名
                //.paths(AppUtility.isProd() ? PathSelectors.none() : PathSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("测试swagger")
                .description("展示swagger界面")
                .termsOfServiceUrl("http://localhost:8081/swagger-ui.html")
                .contact(new Contact("guizimo", "http://localhost:8081/swagger-ui.html", "[email protected]"))
                .version("1.0")
                .build();
    }

}
WebMvcConfig
package com.guizimo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        //排除静态文件

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}

注意导入包的时候别导入错了

配置一个接口

在controller中选择我们的一个接口

package com.guizimo.controller;


import com.guizimo.common.Result;
import com.guizimo.entity.Banner;
import com.guizimo.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 

* banner子项表 前端控制器 *

* * @author guizimo * @since 2020-08-19 */ @RestController @RequestMapping("/banner") @Api(value="轮播图",tags = "轮播图",description = "首页轮播图") public class BannerController { @Autowired BannerService bannerService; // 根据Id查询用户的信息 @ApiOperation(value = "获取轮播图",notes ="获取当前的轮播图") @GetMapping("/index") public Object index(){ Banner banner = bannerService.getById(1L); return Result.success(banner); } }

运行

mvn install

运行SpringBoot项目,运行成功之后

原版swagger-ui http://localhost:8081/swagger-ui.html

SpringBoot搭建API文档插件Swagger并美化_第2张图片

新版swagger-bootstrap-ui http://localhost:8081/doc.html

SpringBoot搭建API文档插件Swagger并美化_第3张图片

感谢

万能的网络

以及勤劳的自己

你可能感兴趣的:(SpringBoot搭建API文档插件Swagger并美化)