springboot集成swagger

ui使用的是swagger-bootstrap-ui

  • 引入依賴

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>swagger-bootstrap-ui</artifactId>
        <version>1.9.4</version>
    </dependency>
    
  • 添加配置類

    import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
    import com.google.common.collect.Lists;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.annotation.Order;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.*;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spi.service.contexts.SecurityContext;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger.web.UiConfiguration;
    import springfox.documentation.swagger.web.UiConfigurationBuilder;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    import java.util.List;
    
    @Configuration
    @EnableSwagger2
    @EnableSwaggerBootstrapUI
    public class Swagger2Config {
    
      @Bean
      public UiConfiguration uiConfiguration(){
          return UiConfigurationBuilder.builder().supportedSubmitMethods(new String[]{})
                  .displayOperationId(true)
                  .build();
      }
    
      @Bean(value = "defaultApi")
      @Order(value = 4)
      public Docket defaultApi(){
          return new Docket(DocumentationType.SWAGGER_2)
                  .apiInfo(apiInfo())
                  .select()
                  .apis(RequestHandlerSelectors.basePackage("com.nanc.modules"))
                  .paths(PathSelectors.any())
                  .build()
                  .globalOperationParameters(parameters())
                  .securityContexts(Lists.newArrayList(securityContext()))
                  .securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
      }
    
      /**
       * 額外添加的請求頭信息
       * @return
       */
      private List<Parameter> parameters(){
          Parameter parameter1 = new ParameterBuilder()
                  .name("token").description("token令牌").modelRef(new ModelRef("String"))
                  .parameterType("header")
                  .required(false).build();
    
          return Lists.<Parameter>newArrayList(parameter1);
      }
    
      private ApiInfo groupApiInfo(){
          return new ApiInfoBuilder()
                  .title("swagger-bootstrap-ui很棒~~~!L干健燕鸽!")
                  .description("<div style='font-size:14px;color:red;'>swagger-bootstrap-ui-demo RESTful APIs</div>")
                  .termsOfServiceUrl("http://www.group.com/")
                  .contact("group@qq.com")
                  .version("1.0")
                  .build();
      }
    
    
      private ApiInfo apiInfo(){
          return new ApiInfoBuilder()
                  .title("swagger-bootstrap-ui RESTful APIs")
                  .description("swagger-bootstrap-ui")
                  .termsOfServiceUrl("http://localhost:8888/")
                  .contact(new Contact("nanc","http://localhost:8888/","nunchan@qq.com"))
                  .version("1.0")
                  .build();
      }
    
      private ApiKey apiKey(){
          return new ApiKey("BearerToken", "Authorization", "header");
      }
    
      private SecurityContext securityContext(){
          return SecurityContext.builder()
                  .securityReferences(defaultAuth())
                  .forPaths(PathSelectors.regex("/.*"))
                  .build();
      }
    
      private List<SecurityReference> defaultAuth(){
          AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
          AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
          authorizationScopes[0] = authorizationScope;
          return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
      }
    }
    
  • 添加配置(在application.yml中配置)

    #swagger的配置
    swagger:
      #正式線上驾讲,修改為true,就無法訪問文檔了
      production: false
      #其他的markdown文檔地址
      markdowns: classpath:swaggermarkdown/*
      basic:
        enable: true
        username: xxx
        password: xxx
    
  • 實體注解類

    @Data
    @TableName("my_test")
    @ApiModel(value = "測試實體類", description = "測試實體類,可以有很長的一段描述信息")
    public class MyTest implements Serializable {
      private static final long serialVersionUID = -7446660454232128458L;
    
      @TableId(value = "id", type = IdType.AUTO)
      private Integer id;
      @ApiModelProperty(value = "手機號", required = true)
      private String mobile;
      private String name;
    }
    
  • 控制層注解類

    @Slf4j
    @RestController
    @RequestMapping("/test")
    @Api(value = "測試", tags = "測試")
    @ApiSort(1)
    public class MyTestController {
      @Autowired
      private MyTestService myTestService;
    
      @ApiResponses({
              @ApiResponse(code=200, message = "新增成功"),
              @ApiResponse(code=404, message = "參數(shù)有誤")})
      @ApiOperation(value = "新增", notes = "新增數(shù)據(jù)", response = Boolean.class)
      @PostMapping("/add")
      public Boolean add(@RequestBody MyTest myTest){
          MyTest mytest = myTestService.add(myTest);
          return mytest.getId() > 0 ? true : false;
      }
    
      @ApiOperation(value = "列表", notes = "查看列表數(shù)據(jù)", responseContainer = "List", response = MyTest.class)
      @GetMapping("/list")
      public List<MyTest> test(){
          List<MyTest> list = myTestService.list();
    
          return list;
      }
    }
    
  • 訪問地址

    http://xxxxx/doc.html
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末绢要,一起剝皮案震驚了整個濱河市屿衅,隨后出現(xiàn)的幾起案子九昧,更是在濱河造成了極大的恐慌尉咕,老刑警劉巖叠蝇,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異年缎,居然都是意外死亡悔捶,警方通過查閱死者的電腦和手機铃慷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜕该,“玉大人犁柜,你說我怎么就攤上這事√玫” “怎么了馋缅?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長绢淀。 經(jīng)常有香客問我萤悴,道長,這世上最難降的妖魔是什么皆的? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任覆履,我火速辦了婚禮,結(jié)果婚禮上费薄,老公的妹妹穿的比我還像新娘硝全。我一直安慰自己,他們只是感情好义锥,可當我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布柳沙。 她就那樣靜靜地躺著岩灭,像睡著了一般拌倍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上噪径,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天柱恤,我揣著相機與錄音,去河邊找鬼找爱。 笑死梗顺,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的车摄。 我是一名探鬼主播寺谤,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吮播!你這毒婦竟也來了变屁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤意狠,失蹤者是張志新(化名)和其女友劉穎粟关,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體环戈,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡闷板,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年澎灸,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遮晚。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡性昭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出县遣,到底是詐尸還是另有隱情巩梢,我是刑警寧澤,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布艺玲,位于F島的核電站括蝠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏饭聚。R本人自食惡果不足惜忌警,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望秒梳。 院中可真熱鬧法绵,春花似錦、人聲如沸酪碘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽兴垦。三九已至徙赢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間探越,已是汗流浹背狡赐。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留钦幔,地道東北人枕屉。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像鲤氢,于是被迫代替她去往敵國和親搀擂。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,974評論 2 355

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