Swagger在线接口插件的简单使用
Swagger
介绍:
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
(大白话:一个可以在线查看API文档,并测试API的插件)
作用:
1. 接口的文档在线自动生成。
2. 功能测试。
环境:springboot2 + swagger2
1)pom.xml添加maven依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2)在springboot启动类同级下,创建swagger的启动类
package com.example.demo;
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;
/**
* Swagger2配置类
* 在与spring boot集成时,放在与Application.java同级的目录下。
* 通过@Configuration注解,让Spring来加载该类配置。
* 再通过@EnableSwagger2注解来启用Swagger2。
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多请关注http://www.baidu.com")
.termsOfServiceUrl("http://www.baidu.com")
.contact("sunf")
.version("1.0")
.build();
}
}
注意:这里需要注意new Docket时,设置扫包路径,这里一定不能填错,不然在接口页面上无法查看到接口
参考:路径出错时 swaggerUI页面没有显示api
4:添加文档内容
在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,描述的主要来源是函数的命名,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容。
Swagger使用的注解及其说明:
@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiImplicitParams : 用在方法上包含一组参数说明。
@ApiImplicitParam:用来注解来给方法入参增加说明。
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
l code:数字,例如400
l message:信息,例如"请求参数没填好"
l response:抛出异常的类
@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
l @ApiModelProperty:描述一个model的属性
注意:@ApiImplicitParam的参数说明:
paramType:指定参数放在哪个地方 |
header:请求参数放置于Request Header,使用@RequestHeader获取 query:请求参数放置于请求地址,使用@RequestParam获取 path:(用于restful接口)-->请求参数的获取:@PathVariable body:(不常用) form(不常用) |
name:参数名 |
|
dataType:参数类型 |
|
required:参数是否必须传 |
true | false |
value:说明参数的意思 |
|
defaultValue:参数的默认值 |
简单测试类:
package com.example.demo.controller;
import com.example.demo.entity.HomeProperties;
import com.example.demo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping(value = "users")
@Api(value = "UserController | 用户操作接口类")
public class UserController {
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());
//获取所有人
@RequestMapping(value = "/", method = RequestMethod.GET)
@ApiOperation(value = "获取所有的用户列表", notes = "注意:需要先有用户信息才能查出列表,需先通过“保存用户”接口保存数据后,才能查出")
public List<User> getUserList(){
List<User> list = new ArrayList<User>(users.values());
return list;
}
//增加
@RequestMapping(value = "/", method = RequestMethod.POST)
@ApiOperation(value = "保存用户", notes = "")
//下面的作用,可以在实体类中设置,那么这里这个接口就不需要在设置了
/*
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "Long"),
@ApiImplicitParam(paramType = "query", name = "name", value = "用户名字", required = true, dataType = "String"),
@ApiImplicitParam(paramType = "query", name = "age", value = "用户年龄", required = true, dataType = "Integer")
})
*/
public String postUser(@ModelAttribute User u){
users.put(u.getId(),u);
return "success";
}
//查找
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ApiOperation(value = "根据用户id查找用户信息", notes = "")
@ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "Long")
public User getUser(@PathVariable Long id){
return users.get(id);
}
//更新
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
@ApiOperation(value = "更新用户信息", notes = "")
@ApiImplicitParam(paramType = "update", name = "id", value = "用户id", required = true, dataType = "Long")
public String putUser(@PathVariable Long id, @ModelAttribute User u){
User user = users.get(id);
user.setName(u.getName());
user.setAge(u.getAge());
users.put(id, user);
return "success";
}
//删除
@ApiOperation(value = "根据用户id删除用户信息", notes = "")
@ApiImplicitParam(paramType = "delete", name = "id", value = "用户id", required = true, dataType = "Long")
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delUser(@PathVariable Long id){
users.remove(id);
return "success";
}
}
POJO对象设置在线属性
package com.example.demo.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
@ApiModel(value = "用户模型")
public class User implements Serializable {
@ApiModelProperty(value = "用户id")
private Long id;
@ApiModelProperty(value = "用户名字")
private String name;
@ApiModelProperty(value = "用户年龄")
private Integer age;
}
启动后,就可以在http://localhost:8080/swagger-ui.html#/查看整个应用的接口,并且可以测试接口
参照: