SpringBoot搭建API文檔插件Swagger並美化

SpringBoot搭建API文檔插件Swagger並美化

博客說明html

文章所涉及的資料來自互聯網整理和我的總結,意在於我的學習和經驗彙總,若有什麼地方侵權,請聯繫本人刪除,謝謝!前端

地址

swagger-bootstrap-ui官網java

導入依賴

<!-- swagger用於定義API文檔 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

<!--美化swagger-->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.3</version>
</dependency>

建立配置文件

image-20200820001317965

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", "2500568424@qq.com"))
                .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);
    }

}

注意導入包的時候別導入錯了git

配置一個接口

在controller中選擇咱們的一個接口github

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;

/**
 * <p>
 * banner子項表 前端控制器
 * </p>
 *
 * @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 installweb

運行SpringBoot項目,運行成功以後spring

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

image-20200820002217679

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

image-20200820002439419

感謝springboot

萬能的網絡

以及勤勞的本身

關注公衆號: 歸子莫,獲取更多的資料,還有更長的學習計劃

相關文章
相關標籤/搜索