目录
- Swagger 介绍
- Swagger 依赖
- SpringBoot 集成 Swagger 配置类
- 常用注解
- 效果示例
Swagger 介绍 【Swagger|Swagger 接口文档】Swagger UI 允许任何人(无论是开发团队还是最终用户)都可以可视化 API 资源并与之交互,而无需任何实现逻辑。
Swagger API 文档是根据 OpenAPI(以前称为 Swagger)规范自动生成的,可简化后端实现和客户端的使用。
Swagger 依赖
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-bean-validators
2.8.0
io.springfox
springfox-swagger-ui
2.9.2
io.swagger
swagger-annotations
1.5.21
io.swagger
swagger-models
1.5.21
SpringBoot 集成 Swagger 配置类
package com.example.apitestplatform.config;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger 文档配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Bean
public Docket docket() {
ParameterBuilder builder=new ParameterBuilder();
builder.parameterType("header")
.name("token")
.description("token值")
.required(true)
.modelRef(new ModelRef("string"));
// 在swagger文档里展示headerreturn new Docket(DocumentationType.SWAGGER_2)
.groupName("ApiDemo")
.apiInfo(apiInfo())
.globalOperationParameters(Lists.newArrayList(builder.build()))
.select()// 选择生成策略
.apis(RequestHandlerSelectors.basePackage("com.example.apitestplatform.controller"))// 选择生成文档的类(忽略该行则不做过滤)
.paths(PathSelectors.any())
.build();
}// 定义接口文档基本信息
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("ApiDemo 系统")// 接口文档标题
.description("ApiDemo 接口文档")// 接口文档描述
.contact(new Contact("xiaoming", "", "103@qq.com"))// 作者联系方式
.version("1.0")// 接口文档版本
.build();
}
}
常用注解
- @Api(tags="API 类标题")
- @ApiOperation(value="https://www.it610.com/article/API 方法标题")
- @ApiModel(value="https://www.it610.com/article/实体类标题", description="实体类描述")
- @ApiModelProperty(value="https://www.it610.com/article/实体属性描述",example="属性示例取值", required=true)
- 接口类:
package com.example.apitestplatform.controller;
import com.example.apitestplatform.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@Api(tags="Swagger Demo 类")
@RestController
@RequestMapping(value="https://www.it610.com/article/demo")// 类中所有接口地址的前缀
public class DemoController {@Value("${user.hobby}")
private String hobby;
@ApiOperation(value="https://www.it610.com/article/Swagger Demo get方法")
// @RequestMapping(value="https://www.it610.com/article/loginGet", method= RequestMethod.GET)
@GetMapping("loginGet")
public String loginGet() {
return hobby;
}// @RequestMapping(value="https://www.it610.com/article/loginPost", method= RequestMethod.POST)
@PostMapping("loginPost")// 简便写法
public String loginPost(@RequestBody User user) {// 如果没用 @RequestBody,则获取结果为 null
System.out.println("username : "+user.getUsername());
System.out.println("password : "+user.getPassword());
return "登录成功:"+user.getUsername();
}// 访问:http://localhost:8080/demo/userId/1/2
// @RequestMapping(value="https://www.it610.com/article/userId/{userId}/{id}", method=RequestMethod.GET)
@GetMapping("getUser/{userid}/{id}")
public String loginUser1(@PathVariable("userid") Integer userid, @PathVariable("id") Integer id) {
System.out.println("userid : "+userid);
System.out.println("id : "+id);
return "userid: "+userid+"id: "+id;
}// 访问:http://localhost:8080/demo/getUser?userid=1&id=2
// 访问:http://localhost:8080/demo/getUser?user=1&id=2,则 userid 值为 null
@GetMapping("getUser")
public String loginUser2(@RequestParam(value="https://www.it610.com/article/userid", required=false) Integer userid,// required=false:参数非必须传
@RequestParam("id") Integer id) {
System.out.println("userid : "+userid);
System.out.println("id : "+id);
return "userid: "+userid+"id: "+id;
}
}
- User 类:
package com.example.apitestplatform.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value="https://www.it610.com/article/用户类", description="用户信息")
@Data
public class User {@ApiModelProperty(value="https://www.it610.com/article/用户名", example="xiaoming", required=true)
private String username;
@ApiModelProperty(value="https://www.it610.com/article/密码", example="123456", required=true)
private String password;
}
效果示例
文章图片