本文测试代码使用 Spring Boot 2.1.6.RELEASE + Swagger 2.9.2

添加依赖

    <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> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

添加配置

swagger:
enable: true

添加配置类

@Configuration
@EnableSwagger2 // 启用 Swagger
@ConditionalOnExpression("${swagger.enable:true}") // 根据配置决定是否最终启用 Swagger
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.karonda.springbootswagger.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SWAGGER 测试平台")
.description("测试 SWAGGER 用")
.termsOfServiceUrl("https://github.com/VictorBu")
.version("1.0.0")
.build();
}
}

添加测试接口

@RestController
@Api(tags = "测试接口")
public class HiController { @RequestMapping(value = "/hi", method = RequestMethod.GET)
@ApiOperation(value="打招呼")
@ApiImplicitParams({
@ApiImplicitParam(name = "firstName", value = "名字", paramType = "query", required = true),
@ApiImplicitParam(name = "lastName", value = "姓氏", paramType = "query", required = true)
})
public String sayHi(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName){
return "Hi, " + firstName + " " + lastName;
}
}

测试

启动程序输入 http://localhost:8080/swagger-ui.html 即可看到效果

注意事项

如果在项目中使用了 Spring Security 则需要添加如下配置

http.authorizeRequests()
.antMatchers(
// -- swagger ui
"/v2/api-docs",
"/swagger-resources",
"/swagger-resources/**",
"/configuration/ui",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**").permitAll()
.anyRequest().authenticated();

完整代码:GitHub

—- 2020-06-10 更新开始 —-

最近使用中发现会报错:

No mapping for GET /swagger-ui.html

应该是 Spring Boot 版本问题,修复:

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport { @Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}

参考:swagger报错No handler found for GET /swagger-ui.html

—- 2020-06-10 更新结束 —-

最新文章

  1. 关于 AVI 的一些代码
  2. 使用dynamic linq 解决自定义查询的若干弊端
  3. MySQL中GROUP_CONCAT中排序
  4. java 20 - 6 加入了异常处理的字节输出流的操作
  5. codeforce The Art of Dealing with ATM
  6. stringlist
  7. createjs 使用记录
  8. JDK 动态代理分析
  9. 一口一口吃掉Volley(一)
  10. 表达式求值(二叉树方法/C++语言描述)(四)
  11. overflow-x: scroll;横向滑动详细讲解
  12. Flask 扩展 Flask-RESTful
  13. 在java中String类为什么要设计成final
  14. 字体图标三种格式区别(Unicode / Font class / Symbol)
  15. 源码管理工具Git-客户端GitBash常用命令
  16. C#延时函数
  17. Axure8.0从入门到精通
  18. python静态属性@property、类方法@classmethod、静态方法@staticmethod和普通方法
  19. MEMS 硅麦资料收集
  20. SSH(Struts、Spring、Hibernate)三大框架整合

热门文章

  1. C语言100题集合004-统计各个年龄阶段的人数
  2. svg究竟是什么?
  3. Preparation for MCM/ICM Writing
  4. php 上传音频文件并获取时长
  5. php 正则金额验证
  6. go-zero 如何扛住流量冲击(一)
  7. MFC窗口通过OpenCV显示图片
  8. Dubbo 接口,导出 Markdown ,这些功能 DocView 现在都有了!
  9. Python_faker (伪装者)创建假数据
  10. CSS z-index应用靠近用户的框