1.前言
随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染、先后端分离的形态,而且前端技术和后端技术在各自的道路上越走越远。
前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,swagger就是一款让你更好的书写API文档的框架,还提供测试页面,让你彻底忘记postman.费话不多说,直接来集成swagger吧
2.创建一个springBoot项目
2.1.pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
</dependency>
</dependencies>
2.2创建一个启动类
@SpringBootApplication
public class SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class,args);
}
}
2.3.创建几个目录如下所示
2.4创建swagger的配置文件(这些配置我觉得你们应该都能看懂,自己按自己的需要改一下就行)
package com.carry.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.carry"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试Swagger api文档")
.description("Swagger api文档")
// .termsOfServiceUrl("/")
.version("1.0")
.build();
}
}
2.5创建一个实体类(我用了lombok,不知道的可以自己添加set,get方法)
package com.carry.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("用户相关")
@Builder
public class User {
@ApiModelProperty(value = "用户姓名",required = false)
private String name;
@ApiModelProperty(value = "用户id",required = false)
private Integer id;
@ApiModelProperty(value = "用户地址",required = true)
private String addr;
}
2.6编写接口(接口都是写死的一些东西,便于测试)
package com.carry.controller;
import com.carry.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api("用户的操作")
public class UserController {
@GetMapping("/user")
@ApiOperation("获取用户")
public String getUser(){
return "1111";
}
@GetMapping("/getUserById/{id}")
@ApiOperation("根据id获取用户")
public User getUserByid(@PathVariable("id") Integer id){
User user = User.builder()
.id(id)
.name("carry")
.addr("上海市浦东新区")
.build();
return user;
}
}
2.7运行启动类,运行 http://localhost:8080/swagger-ui.html (只需要把端口改下就行)
2.8 测试接口(类似postman)
简单不简单,真的很好用,下面我就来介绍下swagger的一些注解的意思,方便大家以后使用(下面只接绍点常用的命令)