当前位置:   article > 正文

SpringBoot使用Swageer文档编辑框架 (狂神)_框架 在线文档加修订

框架 在线文档加修订

Swageer 协调前后端,可测试后端接口,最流行的API框架,RestFul 风格Api文档在线自动生成工具 

swagger 简单使用  

1、导入依赖

  1. <!-- swagger依赖-->
  2. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  3. <dependency>
  4. <groupId>io.springfox</groupId>
  5. <artifactId>springfox-swagger2</artifactId>
  6. <version>3.0.0</version>
  7. </dependency>
  8. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
  9. <dependency>
  10. <groupId>io.springfox</groupId>
  11. <artifactId>springfox-swagger-ui</artifactId>
  12. <version>3.0.0</version>
  13. </dependency>

2.创建Hello工程

3、配置swagger2  

  1. @Configuration
  2. @EnableSwagger2 //开启swagger
  3. public class SwaggerConfiger {
  4. }

运行测试,可通过http://localhost:8080/swagger-ui.html访问

SpringBoot无法访问swagger-ui.html(404)

因为资源路径映射问题,SpringBoot无法找到swagger-ui.html可以添加配置解决

  1. @Configuration
  2. public class WebMvcConfig extends WebMvcConfigurerAdapter {
  3. /**
  4. * 访问静态资源
  5. * */
  6. @Override
  7. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  8. /**
  9. * SpringBoot自动配置本身并不会把/swagger-ui.html
  10. * 这个路径映射到对应的目录META-INF/resources/下面
  11. * 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布;
  12. */
  13. registry.addResourceHandler("swagger-ui.html")
  14. .addResourceLocations("classpath:/META-INF/resources/");
  15. registry.addResourceHandler("/webjars/**")
  16. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  17. //将所有/static/** 访问都映射到classpath:/static/ 目录下
  18. registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
  19. super.addResourceHandlers(registry);
  20. }
  21. }

如果还是不行,检查是否依赖版本过高,测试3.0.0在添加上面的映射路径后还是无效,换低版本后成功。

参考

https://blog.csdn.net/lovequanquqn/article/details/90705721

https://www.cnblogs.com/yuanweidao/p/13710194.html

自定义配置Swagger

  1. @Configuration
  2. @EnableSwagger2 //开启swagger
  3. public class SwaggerConfiger {
  4. @Bean
  5. public Docket docket(){
  6. return new Docket(DocumentationType.SWAGGER_2)
  7. .apiInfo(apiInfo())
  8. // 分组名字
  9. .groupName("看向未来213")
  10. //设置扫描路径,默认全扫描
  11. .select()
  12. // any 扫描全部
  13. //none 不扫描
  14. //basePackage 扫描包
  15. //withMethodAnnotation 按特定方法上注解类扫描
  16. //withClassAnnotation 按特定类注解扫描
  17. .apis(RequestHandlerSelectors.basePackage("com.example.springbootswageer.controller"))
  18. // .paths(PathSelectors.ant("com/**")) //过滤
  19. .build();
  20. }
  21. public ApiInfo apiInfo(){
  22. Contact contact = new Contact("看向未来213", "https://blog.csdn.net/qq_56800327?spm=1000.2115.3001.5343", "2621302586qq.com");
  23. return new ApiInfo(
  24. "Api Documentation",
  25. "Swagger初识",
  26. "v1.0",
  27. "https://blog.csdn.net/qq_56800327?spm=1000.2115.3001.5343",
  28. contact,
  29. "Apache 2.0",
  30. "http://www.apache.org/licenses/LICENSE-2.0",
  31. new ArrayList());
  32. }
  33. }

配置APi文档的分组

  .groupName("看向未来213")

给Api文档添加注释的五个注解

  1. @Api("用户实体类")
  2. @ApiModel("用户实体类")
  3. 两个一样是标注实体类的注解
  4. @ApiModelProperty("用户名") 给属性添加注解
  5. @ApiOperation("方法注释") 给方法添加注解
  6. @ApiParam("用户名") 给参数加注解

try it out 可以在网站上测试,

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/897773
推荐阅读
相关标签
  

闽ICP备14008679号