Swagger學(xué)習(xí)

學(xué)習(xí)地址

https://www.bilibili.com/video/av64841843
https://www.ibm.com/developerworks/cn/java/j-using-swagger-in-a-spring-boot-project/index.html

創(chuàng)建項(xiàng)目引入依賴

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <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>

創(chuàng)建swagger配置類

package com.lv.swaggerdemo.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2//開啟Swagger
public class SwaggerConfig {
}

訪問swagger頁面

訪問:http://localhost:8080/swagger-ui.html

swagger頁面

配置swagger信息

package com.lv.swaggerdemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;


@Configuration
@EnableSwagger2//開啟Swagger
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("lv", "baidu.com", "test@qq.com");
        return new ApiInfo(
                "測試title",
                "測試描述",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }

}

Swagger配置掃描接口

package com.lv.swaggerdemo.config;

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

import java.util.ArrayList;
import java.util.function.Predicate;


@Configuration
@EnableSwagger2//開啟Swagger
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                //配置swagger信息
                .apiInfo(apiInfo())
                //配置是否啟用swagger,false為關(guān)閉
                .enable(false)
                .select()
                //RequestHandlerSelectors配置要掃描接口的方式
                //RequestHandlerSelectors.basePackage指定要掃描的包
                //any()掃描全部
                //none()全部不掃描
                //withMethodAnnotation:掃描方法上的注解
                //withClassAnnotation:掃描類上的注解
                .apis(RequestHandlerSelectors.basePackage("com.lv.swaggerdemo.controller"))
                //paths()膘格。過濾什么路徑(如下:過濾所有/hello開頭的請求)
//                .paths(PathSelectors.ant("/hello/**"))
                .build()
                ;
    }

    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("lv", "baidu.com", "test@qq.com");
        return new ApiInfo(
                "測試title",
                "測試描述",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }

}

設(shè)置生產(chǎn)環(huán)境才啟用swagger

     //設(shè)置要顯示的swagger環(huán)境
        Profiles dev = Profiles.of("dev");
        //通過environment.acceptsProfiles()判斷處在自己設(shè)置的環(huán)境中
        boolean flag = environment.acceptsProfiles(dev);


        return new Docket(DocumentationType.SWAGGER_2)
                //配置swagger信息
                .apiInfo(apiInfo())
                //配置是否啟用swagger彩匕,false為關(guān)閉
                .enable(flag)
                .select()
                //RequestHandlerSelectors配置要掃描接口的方式
                //RequestHandlerSelectors.basePackage指定要掃描的包
                //any()掃描全部
                //none()全部不掃描
                //withMethodAnnotation:掃描方法上的注解
                //withClassAnnotation:掃描類上的注解
                .apis(RequestHandlerSelectors.basePackage("com.lv.swaggerdemo.controller"))
                //paths()。過濾什么路徑(如下:過濾所有/hello開頭的請求)
//                .paths(PathSelectors.ant("/hello/**"))
                .build()
                ;
    }

配置多個(gè)分組

    //如果需要多個(gè)分組堪伍,則配置多個(gè)swagger
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("測試組2");
    }


    @Bean
    public Docket docket(Environment environment) {

        //設(shè)置要顯示的swagger環(huán)境
        Profiles dev = Profiles.of("dev");
        //通過environment.acceptsProfiles()判斷處在自己設(shè)置的環(huán)境中
        boolean flag = environment.acceptsProfiles(dev);


        return new Docket(DocumentationType.SWAGGER_2)
                //配置swagger信息
                .apiInfo(apiInfo())
                //設(shè)置分組
                .groupName("測試組")

接口注釋

controller中

  //只要接口中存在實(shí)體類,它就會被掃描到wagger中
    @PostMapping("/user")
    //ApiOperation接口,描述接口的
    @ApiOperation("Hello控制類")
    public User user(@ApiParam("用戶名") String username){
        return new User();
    }

實(shí)體類中

package com.lv.swaggerdemo.bean;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("用戶實(shí)體類")
public class User {
    @ApiModelProperty("用戶名")
    private String username;
    @ApiModelProperty("密碼")
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

springboot中shiro過濾swagger的請求

網(wǎng)上有些文章是這樣配置的觅闽,但是我測試后不能正常訪問

 filterMap.put("/swagger-ui.html", "anon");
 filterMap.put("/swagger-resources/**", "anon");
 filterMap.put("/v2/**", "anon");
 filterMap.put("/webjars/**", "anon");

改為以下配置后生效帝雇,在shiro配置文件中:

      @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        //不需要權(quán)限也能訪問
        chainDefinition.addPathDefinition("/swagger-ui.html", "anon");
        chainDefinition.addPathDefinition("/swagger-resources", "anon");
        chainDefinition.addPathDefinition("/swagger-resources/configuration/security", "anon");
        chainDefinition.addPathDefinition("/swagger-resources/configuration/ui", "anon");
        chainDefinition.addPathDefinition("/v2/api-docs", "anon");
        chainDefinition.addPathDefinition("/webjars/springfox-swagger-ui/**", "anon");


        //  其他所有頁面必須驗(yàn)證
        chainDefinition.addPathDefinition("/**", "authc");
        return chainDefinition;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蛉拙,隨后出現(xiàn)的幾起案子尸闸,更是在濱河造成了極大的恐慌,老刑警劉巖孕锄,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吮廉,死亡現(xiàn)場離奇詭異,居然都是意外死亡畸肆,警方通過查閱死者的電腦和手機(jī)茧痕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來恼除,“玉大人踪旷,你說我怎么就攤上這事』砘裕” “怎么了令野?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長徽级。 經(jīng)常有香客問我气破,道長,這世上最難降的妖魔是什么餐抢? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任现使,我火速辦了婚禮低匙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘碳锈。我一直安慰自己顽冶,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布售碳。 她就那樣靜靜地躺著强重,像睡著了一般。 火紅的嫁衣襯著肌膚如雪贸人。 梳的紋絲不亂的頭發(fā)上间景,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天,我揣著相機(jī)與錄音艺智,去河邊找鬼倘要。 笑死,一個(gè)胖子當(dāng)著我的面吹牛十拣,可吹牛的內(nèi)容都是我干的封拧。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼父晶,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了弄跌?” 一聲冷哼從身側(cè)響起甲喝,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎铛只,沒想到半個(gè)月后埠胖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡淳玩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年直撤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜕着。...
    茶點(diǎn)故事閱讀 40,664評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谋竖,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出承匣,到底是詐尸還是另有隱情蓖乘,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布韧骗,位于F島的核電站嘉抒,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏袍暴。R本人自食惡果不足惜些侍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一隶症、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧岗宣,春花似錦蚂会、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至纽什,卻和暖如春措嵌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背芦缰。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工企巢, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人让蕾。 一個(gè)月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓浪规,卻偏偏與公主長得像,于是被迫代替她去往敵國和親探孝。 傳聞我的和親對象是個(gè)殘疾皇子笋婿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容