How does spring boot integrate swagger2 and common configurations

  • 的头像-Tumi
  • 16 day agopublish

Ben Pian Wen Zhang Gei Da Jia Fen Xiang De Shi You Guan Springboot Ji Cheng Swagger2 Yi Ji Chang Jian Pei Zhi De Guo Cheng Shi Zen Yang De , Xiao Bian Jue De Ting Shi Yong De , Yin Ci Fen Xiang Gei Da Jia Xue Xi , Xi Wang Da Jia Yue Du Wan Zhe Pian Wen Zhang Hou Ke Yi You Suo Shou Huo , Hua Bu Duo Shuo , Gen Zhuo Xiao Bian Yi Qi Lai Kan Kan Ba 。

  Zhe Zhong Zheng He De Wen Zhang Que Shi Yi Jing Lan Da Jie Liao , Xie Ta Yi Fang Mian Shi Bu Chong Wo De springboot Xi Lie , Ling Yi Fang Mian Que Shi Huan You Yi Bu Fen Xiao Huo Ban Mei Yong Guo 。 Zui Zhong Yao De Shi , Ru Guo Ni Wang Ji Liao Zhe Zhong Zheng He De Dai Ma 。 Ke Yi Sui Shi Cha Yue 。

Qian Yan

Xian Zai De Kai Fa Ji Ben Shang Du Shi Qian Hou Duan Fen Li , Qian Hou Duan Jiao Hu Du Shi Tong Guo API Wen Dang 。 You Liao API Wen Dang Da Jia Ge Zi Kai Fa , Hu Bu Gan Rao 。

1、 Chuan Tong Fang Shi

Chuan Tong Fang Shi Shi Wen Dang She Ji Hao Zhi Hou , Fen Bie Fa Gei Qian Duan He Hou Duan Ren Yuan 。 Zhe Yang You Ge Que Dian , Jie Kou Xin Xi Yi Dan Bian Hua , Wen Dang Jiu Xu Yao Zhong Xin Fa Song Gei Qian Hou Duan Ren Yuan 。 Wu Fa Zuo Dao Shi Shi 。 Suo Yi Lang Fei Shi Jian He Jing Li 。

2、swagger Fang Shi

Wo Men De Hou Tai Ying Yong Ji Cheng Liao swagger Zhi Hou , Hui Zi Dong Bao Lu Chu Wo Men De Jie Kou , Er Qie Zhe Ge Jie Kou Xing Shi Huan Shi Tong Guo restful Feng Ge Fa Bu De 。 Yi Dan Hou Duan De Jie Kou You Bian Hua , Hui Li Ke Xian Shi Chu Lai , Yin Ci Ji Da Di Ti Gao Liao Xiao Lv 。

OK, Ji Ben Shang Yi Ju Hua Jiu Ke Yi Zong Jie Ta De Hao Chu , Na Jiu Shi Hou Duan Xie De api Wen Dang Ke Yi Tong Guo swagger De Xing Shi Shi Shi De Fa Bu Chu Lai , Gong Qian Duan Ren Yuan Cha Kan 。

3、 Qi Ta Fang Shi

swagger De Ye Mian Shuo Shi Hua Chang De Bu Hao Kan , Ye You Yi Xie Qi Ta De Fang An , Bu Shi You Hen Duo bug, Jiu Shi Shou Fei 。 Mu Qian swagger Shi Shi Yong De Zui Duo De 。 Wo Mu Qian Ye Zheng Zai Zuo Zhe Ge Yang De Kai Yuan Xiang Mu , Ji Yu swagger Zuo Chu Lei Si Yu Qi Ta Fang An De Ye Mian , Er Qie Gong Neng Geng Jia De Qiang Da 。

Yi 、 Dai Ma Zheng He

Qian Ti Tiao Jian Shi Yao Xin Jian Yi Ge springboot Xiang Mu 。 Zhe Dian Jiu Bu Yan Shi Liao 。

Di Yi Bu : Tian Jia Yi Lai

<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>

2.9.2 De Ban Ben Shi Yong De Zui Duo De , Ju Ti De Ke Yi Zhi Jie Qu maven De Guan Wang Qu Sou Suo , Zhao Yi Ge Shi Yong Liang Zui Duo De Ban Ben Ji Ke 。

Di Er Bu : Pei Zhi

Xin Jian config Bao , Chuang Jian SwaggerConfig Lei

@EnableSwagger2 @Configuration public class Swagger2Config {     @Bean     public Docket createRestApi() {         return new Docket(DocumentationType.SWAGGER_2)              .apiInfo(apiInfo())              .select()              // Wei  Dang  Qian  Bao  Lu  Jing , Kong  Zhi  Qi  Lei  Bao               .apis(RequestHandlerSelectors.basePackage("com.fdd.controller"))             .paths(PathSelectors.any())              .build();     }     // Gou  Jian  api Wen  Dang  De  Xiang  Xi  Xin  Xi  Han  Shu      private ApiInfo apiInfo() {         return new ApiInfoBuilder()             // Ye  Mian  Biao  Ti             .title("XX Ping  Tai API Jie  Kou  Wen  Dang ")             // Chuang  Jian  Ren             .contact(new Contact(" Feng  Dong  Dong ", "http://www.javachat.cc",                    "3049352171@qq.com"))            // Ban  Ben  Hao            .version("1.0")            // Miao  Shu            .description(" Xi  Tong API Miao  Shu ")           .build();     }

Zhe Li De Pei Zhi Ye Bi Jiao Jian Dan 。 Zhe Li You Hen Duo Xuan Xiang Gong Wo Men Qu Pei Zhi 。 Ru Guo Wo Men De Xiang Mu You Duo Ge Zu , Zhi Xu Yao Chuang Jian Duo Ge Docket Ji Ke 。 Zhe Shi Hou Sao Miao De Bao Huan Cheng Mei Ge Zu De Bao Lu Jing 。

Di San Bu :controller Lei Zhong Pei Zhi

Xin Jian Yi Ge controller Bao , Ran Hou Chuang Jian HelloController Lei

@Api("Hello Kong  Zhi  Lei ") @RestController  public class HelloController {     @GetMapping(value = "/user")     public User getUser(){         return new User(" Yu  Gong  Yao  Yi  Shan ","123456");     }     @ApiOperation(" Ke  Yi  Zhi  Ding  Can  Shu  De API")     @PostMapping("/param")     public String hello2(@ApiParam(" Yong  Hu  Ming ") String name){         return "hello" + name;     } }

Zhe Li Wo Men Ke Yi Kan Chu , Shi Yong Zhu Jie Jiu Ke Yi Dui Zhe Ge Lei 、 Fang Fa 、 Zi Duan Deng Deng Jin Xing Jie Shi Shuo Ming 。 Qi Ta De Zi Duan Huan You Hen Duo , Zai Shi Yong De Shi Hou Hui You Xiang Ying De Ti Shi , Ke Yi Zi Ji Shi Yi Bian :

Springboot Ji  Cheng Swagger2 Yi  Ji  Chang  Jian  Pei  Zhi  De  Guo  Cheng  Shi  Zen  Yang  De

Di Si Bu : Cha Kan Xiao Guo

Fang Wen :http://127.0.0.1:8080/swagger-ui.html Ji Ke 。

OK, Dao Zhe Yi Bu Ji Ben Shang Jiu Ji Cheng Jin Lai Liao 。 Xia Mian Shuo Yi Xia Ke Neng Hui Yu Dao De Pei Zhi 。

San 、 Chang Jian Qi Ta Wen Ti

1、Spring Security – Pei Zhi Mian Ren Zheng Fang Wen

You Shi Hou Wo Men De Springboot Ji Cheng Liao SpringSecurity, Zhe Shi Hou Ru Guo Fang Wen swagger De Di Zhi Hui Zi Dong Tiao Zhuan Dao Deng Lu Ye Mian 。 Zhe Shi Yin Wei SpringSecurity Dui Qi Jin Xing Liao Lan Jie 。 Wei Ci Wo Men Zhi Xu Yao Zai Wo Men De SpringSecurity Pei Zhi Yi Xia Jin Xing Fang Xing Ji Ke 。

Springboot Ji  Cheng Swagger2 Yi  Ji  Chang  Jian  Pei  Zhi  De  Guo  Cheng  Shi  Zen  Yang  De

Xian Zai Pei Zhi Yi Xia , Jin Xing Fang Xing 。 Zai config Bao Xia Xin Jian Yi Ge SpringSecurityConfig Lei

@Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {     @Override     protected void configure(HttpSecurity http) throws Exception {         http                 .authorizeRequests()                 .antMatchers("/swagger-ui.html").permitAll()                 .antMatchers("/webjars/**").permitAll()                 .antMatchers("/swagger-resources/**").permitAll()                 .antMatchers("/v2/*").permitAll()                 .antMatchers("/csrf").permitAll()                 .antMatchers("/").permitAll()                 .anyRequest().authenticated()                 .and()                 .formLogin()         ;     } }

Ci Shi Jiu Ke Yi Zheng Chang De Fang Wen Liao 。

2、 Wei swagger She Zhi jwt

Zhe Zhong Fang Shi Bi Jiao Jian Dan , Zhi Xu Yao Yi Bu Ji Ke 。 Xiu Gai Wo Men De swaggerConfig Lei Ji Ke 。

@EnableSwagger2 @Configuration public class Swagger2Config {     @Bean     public Docket api() {         return new Docket(DocumentationType.SWAGGER_2)                 .apiInfo(apiInfo())                 .securityContexts(Arrays.asList(securityContext()))                 .securitySchemes(Arrays.asList(apiKey()))                 .select()                 .apis(RequestHandlerSelectors.any())                 .paths(PathSelectors.any())                 .build();     }     // Gou  Jian  api Wen  Dang  De  Xiang  Xi  Xin  Xi  Han  Shu      private ApiInfo apiInfo() {         return new ApiInfoBuilder()                 // Ye  Mian  Biao  Ti                  .title("XX Ping  Tai API Jie  Kou  Wen  Dang ")                 // Chuang  Jian  Ren                  .contact(new Contact(" Feng  Dong  Dong ", "http://www.javachat.cc",                         "3049352171@qq.com"))                 // Ban  Ben  Hao                  .version("1.0")                 // Miao  Shu                  .description(" Xi  Tong API Miao  Shu ")                 .build();     }     private ApiKey apiKey() {         return new ApiKey("JWT", "Authorization", "header");     }     private SecurityContext securityContext() {         return SecurityContext.builder().securityReferences(defaultAuth()).build();     }      private List<SecurityReference> defaultAuth() {         AuthorizationScope authorizationScope           = new AuthorizationScope("global", "accessEverything");         AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];         authorizationScopes[0] = authorizationScope;         return Arrays.asList(new SecurityReference("JWT", authorizationScopes));     }  }

Jia Liao Yi Xie token Yan Zheng De Dai Ma , Bi Jiao Jian Dan , Guan Yu JWT De Dong Xi , Ke Yi Si Xia Liao Jie 。 Zhe Li Bu Zhui Shu Liao 。

3、 Yin Cang Endpoint

You Shi Hou Zi Ji Xie De controller, Huo Zhe Shi controller Li Mian De Jie Kou Fang Fa Bu Xiang Rang Qian Duan Ren Yuan Kan Dao , Wo Men Ke Yi Yin Cang Ji Ke 。

Di Yi : Yin Cang Zheng Ge controller

@ApiIgnore @RestController public class MyController {     // Fang  Fa  }

Di Er : Yin Cang Mou Ge Jie Kou Fang Fa 1

@ApiIgnore @ApiOperation(value = " Miao  Shu  Xin  Xi ") @GetMapping("/getAuthor") public String getAuthor() {     return " Yu  Gong  Yao  Yi  Shan "; }

Di San : Yin Cang Mou Ge Jie Kou Fang Fa 2

@ApiOperation(value = " Miao  Shu  Xin  Xi ", hidden = true) @GetMapping("/get") public LocalDate getDate() {     return LocalDate.now(); }

OK, Hen Duo Pei Zhi Ji Ben Shang Jiu Dao Zhe Liao 。

Yi Shang Jiu Shi Springboot Ji Cheng Swagger2 Yi Ji Chang Jian Pei Zhi De Guo Cheng Shi Zen Yang De , Xiao Bian Xiang Xin You Bu Fen Zhi Shi Dian Ke Neng Shi Wo Men Ri Chang Gong Zuo Hui Jian Dao Huo Yong Dao De 。 Xi Wang Ni Neng Tong Guo Zhe Pian Wen Zhang Xue Dao Geng Duo Zhi Shi 。 Geng Duo Xiang Qing Jing Qing Guan Zhu Tumi Yun Xing Ye Zi Xun Pin Dao 。

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment