Springboot集成Swagger2以及常见配置(无坑版)

开发 前端
这种整合的文章确实已经烂大街了,写他一方面是补充我的springboot系列,另一方面确实还有一部分小伙伴没用过。最重要的是,如果你忘记了这种整合的代码。可以随时查阅。

 [[376525]]

这种整合的文章确实已经烂大街了,写他一方面是补充我的springboot系列,另一方面确实还有一部分小伙伴没用过。最重要的是,如果你忘记了这种整合的代码。可以随时查阅。

前言

现在的开发基本上都是前后端分离,前后端交互都是通过API文档。有了API文档大家各自开发,互不干扰。

1、传统方式

传统方式是文档设计好之后,分别发给前端和后端人员。这样有个缺点,接口信息一旦变化,文档就需要重新发送给前后端人员。无法做到实时。所以浪费时间和精力。

2、swagger方式

我们的后台应用集成了swagger之后,会自动暴露出我们的接口,而且这个接口形式还是通过restful风格发布的。一旦后端的接口有变化,会立刻显示出来,因此极大地提高了效率。

OK,基本上一句话就可以总结他的好处,那就是后端写的api文档可以通过swagger的形式实时的发布出来,供前端人员查看。

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的官网去搜索,找一个使用量最多的版本即可。

第二步:配置

新建config包,创建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.     //构建 api文档的详细信息函数 
  15.     private ApiInfo apiInfo() { 
  16.         return new ApiInfoBuilder() 
  17.             //页面标题 
  18.            .title("XX平台API接口文档"
  19.             //创建人 
  20.            .contact(new Contact("冯冬冬""http://www.javachat.cc",   
  21.                  "3049352171@qq.com")) 
  22.            //版本号 
  23.           .version("1.0"
  24.            //描述 
  25.           .description("系统API描述"
  26.           .build(); 
  27.     } 

这里的配置也比较简单。这里有很多选项供我们去配置。如果我们的项目有多个组,只需要创建多个Docket即可。这时候扫描的包换成每个组的包路径。

第三步:controller类中配置

新建一个controller包,然后创建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("可以指定参数的API"
  9.     @PostMapping("/param"
  10.     public String hello2(@ApiParam("用户名") String name){ 
  11.         return "hello" + name
  12.     } 

这里我们可以看出,使用注解就可以对这个类、方法、字段等等进行解释说明。其他的字段还有很多,在使用的时候会有相应的提示,可以自己试一遍:

第四步:查看效果

访问:http://127.0.0.1:8080/swagger-ui.html即可。

这里就是最终的展示效果。OK,到这一步基本上就集成进来了。下面说一下可能会遇到的配置。

三、常见其他问题

1、Spring Security - 配置免认证访问

有时候我们的Springboot集成了SpringSecurity,这时候如果访问swagger的地址会自动跳转到登录页面。这是因为SpringSecurity对其进行了拦截。为此我们只需要在我们的SpringSecurity配置一下进行放行即可。

现在配置一下,进行放行。在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设置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.     //构建 api文档的详细信息函数 
  16.     private ApiInfo apiInfo() { 
  17.         return new ApiInfoBuilder() 
  18.                 //页面标题 
  19.                 .title("XX平台API接口文档"
  20.                 //创建人 
  21.                 .contact(new Contact("冯冬冬""http://www.javachat.cc"
  22.                         "3049352171@qq.com")) 
  23.                 //版本号 
  24.                 .version("1.0"
  25.                 //描述 
  26.                 .description("系统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验证的代码,比较简单,关于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,很多配置基本上就到这了。后续会继续补充。

本文转载自微信公众号「愚公要移山」,可以通过以下二维码关注。转载本文请联系愚公要移山公众号。 

 

责任编辑:武晓燕 来源: 愚公要移山
相关推荐

2024-01-30 08:01:15

RabbitMQ业务逻辑应用场景

2021-05-07 20:27:14

SpringBootSwagger3文档

2021-11-12 16:00:26

鸿蒙HarmonyOS应用

2009-09-08 21:50:23

集成认证测试应用交付控制器Radware

2021-10-28 19:10:02

Go语言编码

2024-04-03 12:30:00

C++开发

2022-02-16 08:21:11

JavaSwagger工具

2010-08-05 09:59:47

DB2常见错误

2023-11-17 18:17:33

微信支付V3版本

2023-12-20 16:26:43

微服务软件开发

2021-07-16 07:57:35

SpringBootOpenFeign微服务

2017-06-20 15:39:58

Koa2 应用动态Swagger文档

2018-02-26 08:59:10

TensorFlow微服务集成机器学习

2019-07-11 10:42:57

容器ArrayList JMH

2019-07-10 08:56:50

Java技术容器

2018-04-23 09:08:12

Windows 语言 系统

2021-04-18 07:33:20

项目Springboot Nacos

2021-05-26 06:22:34

SpringBootJPA后端开发

2021-06-05 07:34:00

SpringBootMybatis用法

2023-01-11 15:11:36

SpringEhcache
点赞
收藏

51CTO技术栈公众号