使用IDEA创建Springboot集成swagger超详细

原创 朱雀 2020-06-19 00:48 7662阅读 10赞

什么是swagger?它有哪些注解?

Swagger介绍
OpenAPI规范(OpenAPI Specification 简称OAS)是Linux基金会的一个项目,试图通过定义一种用来描述API格 式或API定义的语言,来规范RESTful服务开发过程,目前版本是V3.0,并且已经发布并开源在github上。 (https://github.com/OAI/OpenAPI-Specification) Swagger是全球最大的OpenAPI规范(OAS)API开发工具框架,支持从设计和文档到测试和部署的整个API生命周 期的开发。 (https://swagger.io/) Spring Boot 可以集成Swagger,生成Swagger接口,Spring Boot是Java领域的神器,它是Spring项目下快速构建 项目的框架。

常用Swagger注解
在Java类中添加Swagger的注解即可生成Swagger接口,常用Swagger注解如下:
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiModelProperty:用对象接收参数时,描述对 象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用 该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数
@ApiImplicitParam属性:

使用IDEA创建一个Springboot项目

1、首先选择file->New->Project到项目创建页面

2、我们选择Maven点击Next然后来到了下面这个页面

GroupId命名规范一般以cn或者com开头,第二个公司的名称dandelioncloud,我的网站是蒲公英云域名是dandelioncloud.cn所以这里我就使用cn.dandelioncloud

ArtifactId可以填写项目名称,例如demo我这里就不写完整的demo了因为是和swagger集成所以这里只写api

3、修改项目名称以及项目存放地址

projectname是项目的名称。这里我就用api不更改。

project location项目的存放地址。也就是说这个项目放在哪个路径下。我这里就放E盘的project下

然后直接选择完成,我这里选择new Windows 。新建一个窗口。

4、创建好的项目初始化完成后如下图

pom.xml的配置

1、Springboot的配置

因为我们创建项目用的是Maven创建的所以现在只是Maven项目只有加上这段代码才是完整的Springboot项目

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.5.RELEASE</version>
  5. <relativePath/>
  6. </parent>

2、集成swagger需要的相关依赖

上图中完整的依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger2</artifactId>
  9. <version>2.6.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>io.springfox</groupId>
  13. <artifactId>springfox-swagger-ui</artifactId>
  14. <version>2.6.1</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.fasterxml.jackson.core</groupId>
  18. <artifactId>jackson-core</artifactId>
  19. <version>2.6.5</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>com.fasterxml.jackson.core</groupId>
  23. <artifactId>jackson-databind</artifactId>
  24. <version>2.6.5</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>com.fasterxml.jackson.core</groupId>
  28. <artifactId>jackson-annotations</artifactId>
  29. <version>2.6.5</version>
  30. </dependency>
  31. </dependencies>

创建项目启动项

创建APIApplication启动类

  1. package cn.dandelioncloud.api;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class ApiApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ApiApplication.class,args);
  8. }
  9. }

创建swagger配置类

配置类代码效果图

  1. package cn.dandelioncloud.api.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import io.swagger.annotations.ApiOperation;
  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.spi.DocumentationType;
  11. import springfox.documentation.spring.web.plugins.Docket;
  12. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  13. /**
  14. * Swagger配置类
  15. * @author Administrator
  16. */
  17. @Configuration
  18. @EnableSwagger2
  19. public class Swagger2Configuration {
  20. @Bean
  21. public Docket createRestApi(){
  22. return new Docket(DocumentationType.SWAGGER_2)
  23. .apiInfo(apiInfo())
  24. .select()
  25. .paths(PathSelectors.any())
  26. .build();
  27. }
  28. private ApiInfo apiInfo() {
  29. return new ApiInfoBuilder()
  30. .title("测试swagger-蒲公英云")
  31. .description("蒲公英云api文档")
  32. // .termsOfServiceUrl("/")
  33. .version("1.0")
  34. .build();
  35. }
  36. }

创建一个接口用来测试Swagger接口文档

  1. package cn.dandelioncloud.api.controller;
  2. import io.swagger.annotations.Api;
  3. import io.swagger.annotations.ApiImplicitParam;
  4. import io.swagger.annotations.ApiImplicitParams;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. @Api(value="蒲公英云页面管理接口",description = "蒲公英云页面管理接口,提供页面的增、删、改、查")
  10. public interface DandelionCloudController {
  11. final String API_PRE = "/dandelioncloud/page";
  12. @GetMapping(API_PRE+"/list/{page}/{size}")
  13. @ApiOperation("分页查询页面列表")
  14. @ApiImplicitParams({
  15. @ApiImplicitParam(name="page",value = "页码",required=true,paramType="path",dataType="integer"),
  16. @ApiImplicitParam(name="size",value = "每页记录数",required=true,paramType="path",dataType="integer")
  17. })
  18. public ResponseEntity<Object> findList(@PathVariable("page") int page, @PathVariable("size") int size) ;
  19. }

创建一个DTO

  1. 本來想使用lombok发现没有引入依赖那就在加一个依赖吧。

  1. <dependency>
  2. <groupId>org.projectlombok</groupId>
  3. <artifactId>lombok</artifactId>
  4. </dependency>

DTO代碼

  1. package cn.dandelioncloud.api.dto;
  2. import io.swagger.annotations.ApiModelProperty;
  3. import lombok.Data;
  4. import org.springframework.format.annotation.DateTimeFormat;
  5. import java.util.Date;
  6. @Data
  7. public class UserDTO {
  8. @ApiModelProperty(value = "ID")
  9. private Integer id;
  10. @ApiModelProperty(value = "用户登录账号", required = true)
  11. private String userNo;
  12. @ApiModelProperty(value = "姓名", required = true)
  13. private String userName;
  14. @ApiModelProperty(value = "姓名拼音")
  15. private String spellName;
  16. @ApiModelProperty(value = "密码", required = true)
  17. private String password;
  18. @ApiModelProperty(value = "手机号", required = true)
  19. private String userPhone;
  20. @ApiModelProperty(value = "性别")
  21. private Integer userGender;
  22. @ApiModelProperty(value = "记录创建时间")
  23. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  24. private Date createTime;
  25. @ApiModelProperty(value = "记录修改时间")
  26. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  27. private Date updateTime;
  28. }

这样也就算了大功告成了。业务逻辑这里不写,只写接口。

最后在启动类上加上swagger的注解就可以完成启动了

  1. @EnableSwagger2

启动项目

启动成功。端口号8080

最后访问swagger地址

  1. http://localhost:8080/swagger-ui.html
  2. http://localhost:端口号/swagger-ui.html

这里没有写实现类,实现类需要自己写,现在是调不通的。由于时间关系就不提供接口实现类的编写了。期待有机会下次续上。

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 2 条评论,7662人围观)
朱雀
朱雀V铁粉 2021-06-09 17:06
使用IDEA创建Springboot集成swagger超详细
朱雀
朱雀V铁粉 2020-06-19 01:17
世界那么大,我想去看看!

相关阅读

    相关 springboot 集成swagger

    1. 关于swagger 我们撰写的接口文档,有面向内部开发者的,也有面向外部的。很多情况下文档和代码是分离的,有好处也有坏处。而当我们写java项目想偷懒,想要自动生成