自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Springboot集成Swagger2以及常見配置(無坑版)

開發(fā) 前端
這種整合的文章確實已經(jīng)爛大街了,寫他一方面是補充我的springboot系列,另一方面確實還有一部分小伙伴沒用過。最重要的是,如果你忘記了這種整合的代碼??梢噪S時查閱。

 [[376525]]

這種整合的文章確實已經(jīng)爛大街了,寫他一方面是補充我的springboot系列,另一方面確實還有一部分小伙伴沒用過。最重要的是,如果你忘記了這種整合的代碼??梢噪S時查閱。

前言

現(xiàn)在的開發(fā)基本上都是前后端分離,前后端交互都是通過API文檔。有了API文檔大家各自開發(fā),互不干擾。

1、傳統(tǒng)方式

傳統(tǒng)方式是文檔設(shè)計好之后,分別發(fā)給前端和后端人員。這樣有個缺點,接口信息一旦變化,文檔就需要重新發(fā)送給前后端人員。無法做到實時。所以浪費時間和精力。

2、swagger方式

我們的后臺應(yīng)用集成了swagger之后,會自動暴露出我們的接口,而且這個接口形式還是通過restful風格發(fā)布的。一旦后端的接口有變化,會立刻顯示出來,因此極大地提高了效率。

OK,基本上一句話就可以總結(jié)他的好處,那就是后端寫的api文檔可以通過swagger的形式實時的發(fā)布出來,供前端人員查看。

3、其他方式

swagger的頁面說實話長得不好看,也有一些其他的方案,不是有很多bug,就是收費。目前swagger是使用的最多的。我目前也正在做這個樣的開源項目,基于swagger做出類似于其他方案的頁面,而且功能更加的強大。

一、代碼整合

前提條件是要新建一個springboot項目。這點就不演示了。

第一步:添加依賴

  1. <dependency> 
  2.     <groupId>io.springfox</groupId> 
  3.     <artifactId>springfox-swagger2</artifactId> 
  4.     <version>2.9.2</version> 
  5. </dependency> 
  6. <dependency> 
  7.     <groupId>io.springfox</groupId> 
  8.     <artifactId>springfox-swagger-ui</artifactId> 
  9.     <version>2.9.2</version> 
  10. </dependency> 

2.9.2的版本是用的最多的,具體的可以直接去maven的官網(wǎng)去搜索,找一個使用量最多的版本即可。

第二步:配置

新建config包,創(chuàng)建SwaggerConfig類

  1. @EnableSwagger2 
  2. @Configuration 
  3. public class Swagger2Config { 
  4.     @Bean 
  5.     public Docket createRestApi() { 
  6.         return new Docket(DocumentationType.SWAGGER_2) 
  7.              .apiInfo(apiInfo()) 
  8.              .select() 
  9.              //為當前包路徑,控制器類包 
  10.              .apis(RequestHandlerSelectors.basePackage("com.fdd.controller")) 
  11.             .paths(PathSelectors.any()) 
  12.              .build(); 
  13.     } 
  14.     //構(gòu)建 api文檔的詳細信息函數(shù) 
  15.     private ApiInfo apiInfo() { 
  16.         return new ApiInfoBuilder() 
  17.             //頁面標題 
  18.            .title("XX平臺API接口文檔"
  19.             //創(chuàng)建人 
  20.            .contact(new Contact("馮冬冬""http://www.javachat.cc",   
  21.                  "3049352171@qq.com")) 
  22.            //版本號 
  23.           .version("1.0"
  24.            //描述 
  25.           .description("系統(tǒng)API描述"
  26.           .build(); 
  27.     } 

這里的配置也比較簡單。這里有很多選項供我們?nèi)ヅ渲谩H绻覀兊捻椖坑卸鄠€組,只需要創(chuàng)建多個Docket即可。這時候掃描的包換成每個組的包路徑。

第三步:controller類中配置

新建一個controller包,然后創(chuàng)建HelloController類

  1. @Api("Hello控制類"
  2. @RestController  
  3. public class HelloController { 
  4.     @GetMapping(value = "/user"
  5.     public User getUser(){ 
  6.         return new User("愚公要移山","123456"); 
  7.     } 
  8.     @ApiOperation("可以指定參數(shù)的API"
  9.     @PostMapping("/param"
  10.     public String hello2(@ApiParam("用戶名") String name){ 
  11.         return "hello" + name
  12.     } 

這里我們可以看出,使用注解就可以對這個類、方法、字段等等進行解釋說明。其他的字段還有很多,在使用的時候會有相應(yīng)的提示,可以自己試一遍:

第四步:查看效果

訪問:http://127.0.0.1:8080/swagger-ui.html即可。

這里就是最終的展示效果。OK,到這一步基本上就集成進來了。下面說一下可能會遇到的配置。

三、常見其他問題

1、Spring Security - 配置免認證訪問

有時候我們的Springboot集成了SpringSecurity,這時候如果訪問swagger的地址會自動跳轉(zhuǎn)到登錄頁面。這是因為SpringSecurity對其進行了攔截。為此我們只需要在我們的SpringSecurity配置一下進行放行即可。

現(xiàn)在配置一下,進行放行。在config包下新建一個SpringSecurityConfig類

  1. @Configuration 
  2. @EnableWebSecurity 
  3. public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { 
  4.     @Override 
  5.     protected void configure(HttpSecurity http) throws Exception { 
  6.         http 
  7.                 .authorizeRequests() 
  8.                 .antMatchers("/swagger-ui.html").permitAll() 
  9.                 .antMatchers("/webjars/**").permitAll() 
  10.                 .antMatchers("/swagger-resources/**").permitAll() 
  11.                 .antMatchers("/v2/*").permitAll() 
  12.                 .antMatchers("/csrf").permitAll() 
  13.                 .antMatchers("/").permitAll() 
  14.                 .anyRequest().authenticated() 
  15.                 .and() 
  16.                 .formLogin() 
  17.         ; 
  18.     } 

此時就可以正常的訪問了。

2、為swagger設(shè)置jwt

這種方式比較簡單,只需要一步即可。修改我們的swaggerConfig類即可。

  1. @EnableSwagger2 
  2. @Configuration 
  3. public class Swagger2Config { 
  4.     @Bean 
  5.     public Docket api() { 
  6.         return new Docket(DocumentationType.SWAGGER_2) 
  7.                 .apiInfo(apiInfo()) 
  8.                 .securityContexts(Arrays.asList(securityContext())) 
  9.                 .securitySchemes(Arrays.asList(apiKey())) 
  10.                 .select() 
  11.                 .apis(RequestHandlerSelectors.any()) 
  12.                 .paths(PathSelectors.any()) 
  13.                 .build(); 
  14.     } 
  15.     //構(gòu)建 api文檔的詳細信息函數(shù) 
  16.     private ApiInfo apiInfo() { 
  17.         return new ApiInfoBuilder() 
  18.                 //頁面標題 
  19.                 .title("XX平臺API接口文檔"
  20.                 //創(chuàng)建人 
  21.                 .contact(new Contact("馮冬冬""http://www.javachat.cc"
  22.                         "3049352171@qq.com")) 
  23.                 //版本號 
  24.                 .version("1.0"
  25.                 //描述 
  26.                 .description("系統(tǒng)API描述"
  27.                 .build(); 
  28.     } 
  29.     private ApiKey apiKey() { 
  30.         return new ApiKey("JWT""Authorization""header"); 
  31.     } 
  32.     private SecurityContext securityContext() { 
  33.         return SecurityContext.builder().securityReferences(defaultAuth()).build(); 
  34.     } 
  35.  
  36.     private List<SecurityReference> defaultAuth() { 
  37.         AuthorizationScope authorizationScope  
  38.          = new AuthorizationScope("global""accessEverything"); 
  39.         AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; 
  40.         authorizationScopes[0] = authorizationScope; 
  41.         return Arrays.asList(new SecurityReference("JWT", authorizationScopes)); 
  42.     } 
  43.  

加了一些token驗證的代碼,比較簡單,關(guān)于JWT的東西,可以私下了解。這里不贅述了。

3、隱藏Endpoint

有時候自己寫的controller,或者是controller里面的接口方法不想讓前端人員看到,我們可以隱藏即可。

第一:隱藏整個controller

  1. @ApiIgnore 
  2. @RestController 
  3. public class MyController { 
  4.     //方法 

第二:隱藏某個接口方法1

  1. @ApiIgnore 
  2. @ApiOperation(value = "描述信息"
  3. @GetMapping("/getAuthor"
  4. public String getAuthor() { 
  5.     return "愚公要移山"

第三:隱藏某個接口方法2

  1. @ApiOperation(value = "描述信息", hidden = true
  2. @GetMapping("/get"
  3. public LocalDate getDate() { 
  4.     return LocalDate.now(); 

OK,很多配置基本上就到這了。后續(xù)會繼續(xù)補充。

本文轉(zhuǎn)載自微信公眾號「愚公要移山」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系愚公要移山公眾號。 

 

責任編輯:武曉燕 來源: 愚公要移山
相關(guān)推薦

2024-01-30 08:01:15

RabbitMQ業(yè)務(wù)邏輯應(yīng)用場景

2021-05-07 20:27:14

SpringBootSwagger3文檔

2021-11-12 16:00:26

鴻蒙HarmonyOS應(yīng)用

2009-09-08 21:50:23

集成認證測試應(yīng)用交付控制器Radware

2021-10-28 19:10:02

Go語言編碼

2024-04-03 12:30:00

C++開發(fā)

2010-08-05 09:59:47

DB2常見錯誤

2022-02-16 08:21:11

JavaSwagger工具

2025-02-21 09:52:14

2023-11-17 18:17:33

微信支付V3版本

2023-12-20 16:26:43

微服務(wù)軟件開發(fā)

2021-07-16 07:57:35

SpringBootOpenFeign微服務(wù)

2017-06-20 15:39:58

Koa2 應(yīng)用動態(tài)Swagger文檔

2018-02-26 08:59:10

TensorFlow微服務(wù)集成機器學習

2019-07-10 08:56:50

Java技術(shù)容器

2019-07-11 10:42:57

容器ArrayList JMH

2018-04-23 09:08:12

Windows 語言 系統(tǒng)

2021-04-18 07:33:20

項目Springboot Nacos

2021-05-26 06:22:34

SpringBootJPA后端開發(fā)

2021-06-05 07:34:00

SpringBootMybatis用法
點贊
收藏

51CTO技術(shù)棧公眾號