使用IDEA创建Springboot集成swagger超详细 原创 朱雀 2020-06-19 00:48 7600阅读 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项目 ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> </parent> ``` #### 2、集成swagger需要的相关依赖  上图中完整的依赖 ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.5</version> </dependency> </dependencies> ``` ## 创建项目启动项  创建APIApplication启动类  ```java package cn.dandelioncloud.api; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ApiApplication { public static void main(String[] args) { SpringApplication.run(ApiApplication.class,args); } } ``` ## 创建swagger配置类  配置类代码效果图  ```java package cn.dandelioncloud.api.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import io.swagger.annotations.ApiOperation; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Swagger配置类 * @author Administrator */ @Configuration @EnableSwagger2 public class Swagger2Configuration { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("测试swagger-蒲公英云") .description("蒲公英云api文档") // .termsOfServiceUrl("/") .version("1.0") .build(); } } ``` ## 创建一个接口用来测试Swagger接口文档   ```java package cn.dandelioncloud.api.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @Api(value="蒲公英云页面管理接口",description = "蒲公英云页面管理接口,提供页面的增、删、改、查") public interface DandelionCloudController { final String API_PRE = "/dandelioncloud/page"; @GetMapping(API_PRE+"/list/{page}/{size}") @ApiOperation("分页查询页面列表") @ApiImplicitParams({ @ApiImplicitParam(name="page",value = "页码",required=true,paramType="path",dataType="integer"), @ApiImplicitParam(name="size",value = "每页记录数",required=true,paramType="path",dataType="integer") }) public ResponseEntity<Object> findList(@PathVariable("page") int page, @PathVariable("size") int size) ; } ``` ## 创建一个DTO   ``` 本來想使用lombok发现没有引入依赖那就在加一个依赖吧。 ```  ```xml <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> ``` DTO代碼 ```java package cn.dandelioncloud.api.dto; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; @Data public class UserDTO { @ApiModelProperty(value = "ID") private Integer id; @ApiModelProperty(value = "用户登录账号", required = true) private String userNo; @ApiModelProperty(value = "姓名", required = true) private String userName; @ApiModelProperty(value = "姓名拼音") private String spellName; @ApiModelProperty(value = "密码", required = true) private String password; @ApiModelProperty(value = "手机号", required = true) private String userPhone; @ApiModelProperty(value = "性别") private Integer userGender; @ApiModelProperty(value = "记录创建时间") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; @ApiModelProperty(value = "记录修改时间") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; } ``` 这样也就算了大功告成了。业务逻辑这里不写,只写接口。 最后在启动类上加上swagger的注解就可以完成启动了 ```java @EnableSwagger2 ```  启动项目  启动成功。端口号8080  最后访问swagger地址 ```html http://localhost:8080/swagger-ui.html http://localhost:端口号/swagger-ui.html ```  这里没有写实现类,实现类需要自己写,现在是调不通的。由于时间关系就不提供接口实现类的编写了。期待有机会下次续上。 文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。
相关 超详细IDEA创建Maven项目 文章目录 一、Maven概述 二、创建Maven项目 三、Maven项目简单介绍 3.1 标准化的项目结构 迈不过友情╰/ 2023年10月12日 12:32/ 0 赞/ 72 阅读
相关 超详细IDEA创建MavenWeb项目 文章目录 一、环境准备 二、骨架方式创建Maven-Web项目 三、非骨架方式创建Maven-Web项目 一、环境准备 > 电玩女神/ 2023年10月12日 09:54/ 0 赞/ 71 阅读
相关 SpringBoot集成Swagger的详细步骤 目录 一、简介以及使用 二、整合步骤 三、注解说明 一、简介以及使用 号称:世界最流行的API框架 官网:http://swagger.io/ 解决什么 谁借莪1个温暖的怀抱¢/ 2023年02月11日 13:29/ 0 赞/ 9 阅读
相关 SpringBoot项目集成使用Swagger 一、简介 前后端分离 后端时代: 前端只用管理静态页面;html==>后端(前端编写HTML等样式交给后端),模板引擎JSP=>后端是主力 我不是女神ヾ/ 2022年12月02日 10:54/ 0 赞/ 224 阅读
相关 SpringBoot集成使用jsp(超详细) SpringBoot集成使用jsp 添加依赖 添加上下文目录 指定SpringBoot的启动目录 设置application.properties 朱雀/ 2022年11月16日 00:57/ 0 赞/ 616 阅读
相关 springboot 集成 Swagger 优点: (1)aio文档与api定义同步更新 (2)直接运行,可在线测试 (3)支持多种语言 maven下载依赖 Swagger2是新版的。 ╰+哭是因爲堅強的太久メ/ 2022年10月12日 04:19/ 0 赞/ 291 阅读
相关 SpringBoot 集成swagger Swagger Swagger的功能:一款可以生成,调用,可视化api的框架。让你不再忘记接口长啥样的工具,让不再为测试而烦恼,因为Swagger集成了这些功能,而且你还 曾经终败给现在/ 2022年05月14日 16:13/ 0 赞/ 449 阅读
相关 springboot集成swagger [springboot技术栈][springboot] [swagger官网][swagger] swagger注解说明 <table> <thead> Myth丶恋晨/ 2022年03月15日 11:11/ 0 赞/ 543 阅读
相关 springboot 集成swagger 1. 关于swagger 我们撰写的接口文档,有面向内部开发者的,也有面向外部的。很多情况下文档和代码是分离的,有好处也有坏处。而当我们写java项目想偷懒,想要自动生成 淡淡的烟草味﹌/ 2022年02月12日 11:49/ 0 赞/ 492 阅读
相关 使用IDEA创建Springboot集成swagger超详细 什么是swagger?它有哪些注解? Swagger介绍 OpenAPI规范(OpenAPI Specification 简称OAS)是Linux基金会的一个项目,试图通... 朱雀/ 2020年06月19日 00:48/ 10 赞/ 7601 阅读