当前位置:   article > 正文

Swagger API在线文档框架应用_swagger-resources doc.html

swagger-resources doc.html

本教程版本使用2.9.2

1、导入maven依赖

  1. <!-- swagger -->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.9.2</version>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>io.swagger</groupId>
  9. <artifactId>swagger-models</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>
  13. <dependency>
  14. <groupId>io.swagger</groupId>
  15. <artifactId>swagger-models</artifactId>
  16. <version>1.5.21</version>
  17. </dependency>
  18. <!-- swagger-ui -->
  19. <dependency>
  20. <groupId>io.springfox</groupId>
  21. <artifactId>springfox-swagger-ui</artifactId>
  22. <version>2.9.2</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.github.xiaoymin</groupId>
  26. <artifactId>swagger-bootstrap-ui</artifactId>
  27. <version>1.8.5</version>
  28. </dependency>

2、SwaggerConfig设置

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  6. import io.swagger.annotations.ApiOperation;
  7. import springfox.documentation.builders.ApiInfoBuilder;
  8. import springfox.documentation.builders.ParameterBuilder;
  9. import springfox.documentation.builders.RequestHandlerSelectors;
  10. import springfox.documentation.schema.ModelRef;
  11. import springfox.documentation.service.ApiInfo;
  12. import springfox.documentation.service.Contact;
  13. import springfox.documentation.service.Parameter;
  14. import springfox.documentation.spi.DocumentationType;
  15. import springfox.documentation.spring.web.plugins.Docket;
  16. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  17. @Configuration
  18. @EnableSwagger2
  19. public class SwaggerConfig extends WebMvcConfigurerAdapter{
  20. @Bean
  21. public Docket swaggerSpringMvcPlugin() {
  22. //添加head参数start
  23. ParameterBuilder ticketPar = new ParameterBuilder();
  24. List<Parameter> pars = new ArrayList<Parameter>();
  25. ticketPar.name("token").description("token")
  26. .modelRef(new ModelRef("String")).parameterType("header")
  27. .required(false).build();
  28. pars.add(ticketPar.build());
  29. //添加head参数end
  30. return new Docket(DocumentationType.SWAGGER_2)
  31. .apiInfo(apiInfo())
  32. .select()
  33. .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
  34. .apis(RequestHandlerSelectors.basePackage("com.enw"))
  35. .build().globalOperationParameters(pars);
  36. }
  37. private ApiInfo apiInfo(){
  38. return new ApiInfoBuilder()
  39. .title("API接口文档")
  40. .description("CRM管理系统API接口文档")
  41. .contact(new Contact("", "", ""))
  42. .version("1.0")
  43. .build();
  44. }
  45. }

3、具体使用

  1. @Api(tags="角色控制器")
  2. @RestController
  3. @RequestMapping("/role/")
  4. public class RoleController extends BaseController {
  5. @Autowired
  6. private RoleService roleService;
  7. @SuppressWarnings("unchecked")
  8. @ApiOperation(value = "获取角色列表" , notes="获取角色列表")
  9. @ApiImplicitParam(name = "role", value = "角色信息", required = false, dataType = "Role")
  10. @PostMapping("list")
  11. @UserLoginToken
  12. public JsonResult<Role> roleList(@RequestBody Role role) {
  13. List<Role> list = this.roleService.findAllRole(role);
  14. return JsonResult.success(list);
  15. }
  16. }

 4、访问 localhost:端口号/doc.html  效果如下:

注意:

1、如果有拦截器需要放开的路径:

"doc.html","/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"

2、一般前后端分离,前端的token都是封装到请求头header中的,设置统一请求头token

  1. //添加head参数start
  2. ParameterBuilder ticketPar = new ParameterBuilder();
  3. List<Parameter> pars = new ArrayList<Parameter>();
  4. ticketPar.name("token").description("token")
  5. .modelRef(new ModelRef("String")).parameterType("header")
  6. .required(false).build();
  7. pars.add(ticketPar.build());
  8. //添加head参数end

3、今天使用doc.html访问时后台报错:java.lang.NumberFormatException: For input string: "".原因是:

由于我pom引入的io.springfox:springfox-swagger-ui:2.9.2版本的jar包内置为io.swagger:swagger-models包为1.5.20版本。1.5.20版本中判断example只判断是否为null,没有判断example为空字符串""的情况所以报错.1.5.21版本新增了判断example是否为null和"".

解决办法:所以排除1.5.20包重新导入1.5.21包即可解决,具体依赖如下(标红部分注意):

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
        <exclusions>
            <exclusion>
                   <groupId>io.swagger</groupId>
                <artifactId>swagger-models</artifactId>
            </exclusion>
        </exclusions>

    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-models</artifactId>
        <version>1.5.21</version>
    </dependency>

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

闽ICP备14008679号