1、導(dǎo)入maven工程
首先我們創(chuàng)建一個 Spring Boot 項目,并引入 Swagger3 的核心依賴包,如下:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、核心配置
接下來我們在啟動類上添加兩個注解,開啟Swagger功能。
//開啟swagger
@EnableSwagger2
@EnableOpenApi
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
3、啟動項目
接下來讓我們可以啟動項目,然后在瀏覽器中輸入如下地址:
http://localhost:8085/swagger-ui/index.html
注意,端口是自己tomcat啟動時的端口,以自己電腦的為準(zhǔn)
4、進(jìn)入界面
5、swagger配置類
package com.swagger.config;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
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 SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("用戶組")
.select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.ant("/swagger/**"))
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Api接口文檔")
.description("API描述")
.version("1.0") .termsOfServiceUrl("https://www.baidu.com") .build(); }
}
6、Controller接口配置
@Api(tags = "用戶控制")代表對這個controller的描述
@ApiOperation(value = "查詢所有用戶", notes = "查詢所有用戶信息")代表對接口的描述
package com.swagger.controller;
import com.baomidou.mybatisplus.extension.api.R;
import com.swagger.domain.User;
import com.swagger.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "用戶控制")
@RestController
@RequestMapping("/swagger/user")
@CrossOrigin
@Slf4j
public class UserController {
@Autowired
private UserServiceImpl userService;
@GetMapping("/selectAll")
@ResponseBody
@ApiOperation(value = "查詢所有用戶", notes = "查詢所有用戶信息")
public R selectAll(){
List<User> list = userService.list();
System.out.println(list);
return R.ok(list).setCode(200);
}
@PostMapping("/save")
@ApiOperation(value = "新增用戶", notes = "新增用戶信息")
public R save(@RequestBody User user){
return R.ok("success").setCode(200);
}
@PutMapping("/update")
@ApiOperation(value = "修改用戶", notes = "修改用戶信息")
public R update(@RequestBody User user){
return R.ok("success").setCode(200);
}
@DeleteMapping("/delete")
@ApiOperation(value = "刪除用戶", notes = "刪除用戶信息")
public R delete(int id){
return R.ok("success").setCode(200);
}
}
7、實體類配置
@ApiModel屬性:description:用于描述實體類
@ApiModel(value = "用戶實體",description = "用戶實體")
@ApiModelProperty屬性:notes:描述該實體類屬性的信息
@ApiModelProperty(notes = "用戶Id")
代碼演示
package com.swagger.domain;
import com.baomidou.mybatisplus.annotation.*;
import java.io.Serializable;
import java.util.Date;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 用戶
* @TableName pd_auth_user
*/
@TableName(value ="pd_auth_user")
@Data
@ApiModel(value = "用戶實體",description = "用戶實體")
public class User implements Serializable {
/**
* ID
*/
@ApiModelProperty(notes = "用戶Id")
@TableId(value = "id")
private Long id;
/**
* 賬號
*/
@ApiModelProperty(notes = "賬號")
@TableField(value = "account")
private String account;
/**
* 姓名
*/
@ApiModelProperty(notes = "姓名")
@TableField(value = "name")
private String name;
/**
* 組織ID
#c_core_org
*/
@ApiModelProperty(notes = "組織ID")
@TableField(value = "org_id")
private Long org_id;
/**
* 崗位ID
#c_core_station
*/
@ApiModelProperty(notes = "崗位ID")
@TableField(value = "station_id")
private Long station_id;
/**
* 郵箱
*/
@ApiModelProperty(notes = "郵箱")
@TableField(value = "email")
private String email;
/**
* 手機(jī)
*/
@ApiModelProperty(notes = "手機(jī)")
@TableField(value = "mobile")
private String mobile;
/**
* 性別
#Sex{W:女;M:男;N:未知}
*/
@ApiModelProperty(notes = "性別 W:女;M:男;N:未知")
@TableField(value = "sex")
private String sex;
/**
* 啟用狀態(tài) 1啟用 0禁用
*/
@ApiModelProperty(notes = "啟用狀態(tài) 1啟用 0禁用")
@TableField(value = "status")
private Boolean status;
/**
* 頭像
*/
@ApiModelProperty(notes = "頭像")
@TableField(value = "avatar")
private String avatar;
/**
* 工作描述
比如: 市長、管理員、局長等等 用于登陸展示
*/
@ApiModelProperty(notes = "工作描述\n" +
"比如: 市長、管理員、局長等等 用于登陸展示")
@TableField(value = "work_describe")
private String work_describe;
/**
* 最后一次輸錯密碼時間
*/
@ApiModelProperty(notes = "最后一次輸錯密碼時間")
@TableField(value = "password_error_last_time")
private Date password_error_last_time;
/**
* 密碼錯誤次數(shù)
*/
@ApiModelProperty(notes = "密碼錯誤次數(shù)")
@TableField(value = "password_error_num")
private Integer password_error_num;
/**
* 密碼過期時間
*/
@ApiModelProperty(notes = "密碼過期時間")
@TableField(value = "password_expire_time")
private Date password_expire_time;
/**
* 密碼
*/
@ApiModelProperty(notes = "密碼")
@TableField(value = "password")
private String password;
/**
* 最后登錄時間
*/
@ApiModelProperty(notes = "最后登錄時間")
@TableField(value = "last_login_time")
private Date last_login_time;
/**
* 創(chuàng)建人id
*/
@ApiModelProperty(notes = "創(chuàng)建人id")
@TableField(value = "create_user")
private Long create_user;
/**
* 創(chuàng)建時間
*/
@ApiModelProperty(notes = "創(chuàng)建時間")
@TableField(value = "create_time",fill = FieldFill.INSERT)
private Date create_time;
/**
* 更新人id
*/
@ApiModelProperty(notes = "更新人id")
@TableField(value = "update_user")
private Long update_user;
/**
* 更新時間
*/
@ApiModelProperty(notes = "更新時間")
@TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
private Date update_time;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
swagger顯示
8、注解