SpringBoot整合Swagger开发-SpringBoot2.*整合Swagger2开发-《Java笔记》

admin 2025-10-19 05:29:23 编程 来源:ZONE.CI 全球网 0 阅读模式

Java SpringBoot Swagger

1、导入Swagger2依赖

  1. <springfox-swagger.version>2.8.0</springfox-swagger.version>
  2. <swagger-bootstrap-ui.version>1.7.2</swagger-bootstrap-ui.version>
  3. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  4. <dependency>
  5. <groupId>io.springfox</groupId>
  6. <artifactId>springfox-swagger2</artifactId>
  7. <version>${springfox-swagger.version}</version>
  8. </dependency>
  9. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
  10. <dependency>
  11. <groupId>io.springfox</groupId>
  12. <artifactId>springfox-swagger-ui</artifactId>
  13. <version>${springfox-swagger.version}</version>
  14. </dependency>
  15. <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui -->
  16. <dependency>
  17. <groupId>com.github.xiaoymin</groupId>
  18. <artifactId>swagger-bootstrap-ui</artifactId>
  19. <version>${swagger-bootstrap-ui.version}</version>
  20. </dependency>

2、配置Swagger

SwaggerConfig.java

  1. package com.fcant.service_acti.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  5. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  6. import springfox.documentation.builders.ApiInfoBuilder;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.service.Contact;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. /**
  15. * SwaggerConfig
  16. * <p>
  17. * encoding:UTF-8
  18. *
  19. * @author Fcant 下午 23:41 2020/8/1/0001
  20. */
  21. @Configuration
  22. @EnableSwagger2
  23. public class SwaggerConfig implements WebMvcConfigurer {
  24. @Override
  25. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  26. registry.addResourceHandler("swagger-ui.html")
  27. .addResourceLocations("classpath:/META-INF/resources/");
  28. registry.addResourceHandler("/webjars/**")
  29. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  30. }
  31. /**
  32. * @author Fcant 13:44 2019/12/5
  33. */
  34. @Bean
  35. public Docket petApi() {
  36. return new Docket(DocumentationType.SWAGGER_2)
  37. .apiInfo(apiInfo())
  38. .select()
  39. .apis(RequestHandlerSelectors.basePackage("com.fcant.service_acti.controller"))
  40. .paths(PathSelectors.any())
  41. .build();
  42. }
  43. /**
  44. * 该套 API 说明,包含作者、简介、版本、host、服务URL
  45. * @return
  46. */
  47. private ApiInfo apiInfo() {
  48. return new ApiInfoBuilder()
  49. .title("Activiti Service API")
  50. .contact(new Contact("fcant","null","[email protected]"))
  51. .version("0.1")
  52. .termsOfServiceUrl("localhost:8080/swagger")
  53. .description("Activiti Service API")
  54. .build();
  55. }
  56. }

3、访问

在浏览器地址输入-localhost:8080/swagger-ui.html进入image.png

4、Swagger注解总结

A.官方总结

Swagger相关注解官方说明地址https://github.com/swagger-api/swagger-core/wiki/Annotations

B.常见总结

@Api()用于类

标识这个类是swagger的资源  tags–表示分组说明标签

@ApiOperation()用于方法

表示一个http请求的操作  value用于方法描述   notes用于提示内容

@ApiModel()用于实体类

表示对类进行说明,用于参数用实体类接收 value–表示对象名 description–描述

@ApiModelProperty()用于实体类字段

表示对model属性的说明或者数据操作更改  value–字段说明   name–重写属性名字   dataType–重写属性类型   required–是否必填   example–举例说明   hidden–隐藏

@ApiImplicitParam() 用于 controller 方法

表示单独的请求参数  name–参数ming  value–参数说明  dataType–数据类型  paramType–参数类型  example–举例说明

@ApiImplicitParams() 用于 controller 方法

包含多个 @ApiImplicitParam

@ApiIgnore()用于类或者方法上

可以不被swagger显示在页面上

以太坊cppgolang区别 编程

以太坊cppgolang区别

以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
progolang 编程

progolang

Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
golangn个发送者 编程

golangn个发送者

Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
golang技能图谱 编程

golang技能图谱

从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
评论:0   参与:  6