springboot 之集成Swagger2

前言

一直沒(méi)機(jī)會(huì)做spring生態(tài)圈的框架,公司選擇的是一些小眾的微服務(wù),鑒于此考慮,豐富自己的技術(shù)棧,花了兩天時(shí)間從網(wǎng)上各網(wǎng)站上學(xué)習(xí)了springboot一些基礎(chǔ)知識(shí)。
本章只介紹springboot微服務(wù)集成swagger2,用于后端工程師開(kāi)發(fā)過(guò)程中調(diào)試接口。

環(huán)境準(zhǔn)備

  • IntelliJ IDEA
  • 前一章中搭建的微服務(wù)框架

開(kāi)始集成

  1. pom.xml中增加依賴(lài)包


    依賴(lài)包.png
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
  1. 引入swaggerUI靜態(tài)資源文件,將資源拷貝到resources/static目錄下,否則會(huì)報(bào)404,找不到資源,資源下載路徑見(jiàn)://download.csdn.net/download/xuefei2290819/12043546

    swagger靜態(tài)資源.png

  2. 與DemoApplication.java同級(jí)目錄新建SwaggerConfig類(lèi)


    SwaggerConfig.png
package com.example.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * 類(lèi)功能描述:<br>
 * <ul>
 * <li>類(lèi)功能描述1<br>
 * <li>類(lèi)功能描述2<br>
 * <li>類(lèi)功能描述3<br>
 * </ul>
 * 修改記錄:<br>
 * <ul>
 * <li>修改記錄描述1<br>
 * <li>修改記錄描述2<br>
 * <li>修改記錄描述3<br>
 * </ul>
 *
 * @author xuefl
 * @version 5.0 since 2019-12-19
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        // 解決 SWAGGER 404報(bào)錯(cuò)
        registry.addResourceHandler("/static/index.html").addResourceLocations("classpath:/META-INF/resources/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//生成文檔的api對(duì)象定義
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//掃描生成文檔的包路徑
                //.paths(PathSelectors.ant("/*Api/*"))//生成文檔的類(lèi)訪(fǎng)問(wèn)路徑,就是controller類(lèi)里@RequestMapping("orderApi")
                .paths(PathSelectors.any())
                .build();
        //.host(Host);//配置swagger前綴
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot-demo構(gòu)建restful接口api")//文檔標(biāo)題
                .description("此API提供接口調(diào)用")//文檔說(shuō)明
                .version("2.0").build();//版本號(hào)
    }
}
  1. DemoApplication中增加@EnableSwagger2注解


    EnableSwagger2.png
package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@MapperScan("com.example.demo.mapper")
@EnableScheduling
@EnableTransactionManagement
@Slf4j
public class DemoApplication {

    public static void main(String[] args) {
        log.info("DemoApplication run enter...");
        SpringApplication.run(DemoApplication.class, args);
    }

}
  1. 運(yùn)行DemoApplication,啟動(dòng)服務(wù),訪(fǎng)問(wèn) http://localhost:8081/api/
    運(yùn)行結(jié)果.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過(guò)簡(jiǎn)信或評(píng)論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容