1、導(dǎo)入maven工程
首先我們創(chuàng)建一個(gè) Spring Boot 項(xiàng)目,并引入 Swagger3 的核心依賴(lài)包,如下:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、核心配置
接下來(lái)我們?cè)趩?dòng)類(lèi)上添加兩個(gè)注解,開(kāi)啟Swagger功能。
//開(kāi)啟swagger
@EnableSwagger2
@EnableOpenApi
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
3、啟動(dòng)項(xiàng)目
接下來(lái)讓我們可以啟動(dòng)項(xiàng)目,然后在瀏覽器中輸入如下地址:
http://localhost:8085/swagger-ui/index.html
注意,端口是自己tomcat啟動(dòng)時(shí)的端口,以自己電腦的為準(zhǔn)
4、進(jìn)入界面
5、swagger配置類(lèi)
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("用戶(hù)組")
.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 = "用戶(hù)控制")代表對(duì)這個(gè)controller的描述
@ApiOperation(value = "查詢(xún)所有用戶(hù)", notes = "查詢(xún)所有用戶(hù)信息")代表對(duì)接口的描述
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 = "用戶(hù)控制")
@RestController
@RequestMapping("/swagger/user")
@CrossOrigin
@Slf4j
public class UserController {
@Autowired
private UserServiceImpl userService;
@GetMapping("/selectAll")
@ResponseBody
@ApiOperation(value = "查詢(xún)所有用戶(hù)", notes = "查詢(xún)所有用戶(hù)信息")
public R selectAll(){
List<User> list = userService.list();
System.out.println(list);
return R.ok(list).setCode(200);
}
@PostMapping("/save")
@ApiOperation(value = "新增用戶(hù)", notes = "新增用戶(hù)信息")
public R save(@RequestBody User user){
return R.ok("success").setCode(200);
}
@PutMapping("/update")
@ApiOperation(value = "修改用戶(hù)", notes = "修改用戶(hù)信息")
public R update(@RequestBody User user){
return R.ok("success").setCode(200);
}
@DeleteMapping("/delete")
@ApiOperation(value = "刪除用戶(hù)", notes = "刪除用戶(hù)信息")
public R delete(int id){
return R.ok("success").setCode(200);
}
}
7、實(shí)體類(lèi)配置
@ApiModel屬性:description:用于描述實(shí)體類(lèi)
@ApiModel(value = "用戶(hù)實(shí)體",description = "用戶(hù)實(shí)體")
@ApiModelProperty屬性:notes:描述該實(shí)體類(lèi)屬性的信息
@ApiModelProperty(notes = "用戶(hù)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;
/**
* 用戶(hù)
* @TableName pd_auth_user
*/
@TableName(value ="pd_auth_user")
@Data
@ApiModel(value = "用戶(hù)實(shí)體",description = "用戶(hù)實(shí)體")
public class User implements Serializable {
/**
* ID
*/
@ApiModelProperty(notes = "用戶(hù)Id")
@TableId(value = "id")
private Long id;
/**
* 賬號(hào)
*/
@ApiModelProperty(notes = "賬號(hào)")
@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;
/**
* 工作描述
比如: 市長(zhǎng)、管理員、局長(zhǎng)等等 用于登陸展示
*/
@ApiModelProperty(notes = "工作描述\n" +
"比如: 市長(zhǎng)、管理員、局長(zhǎng)等等 用于登陸展示")
@TableField(value = "work_describe")
private String work_describe;
/**
* 最后一次輸錯(cuò)密碼時(shí)間
*/
@ApiModelProperty(notes = "最后一次輸錯(cuò)密碼時(shí)間")
@TableField(value = "password_error_last_time")
private Date password_error_last_time;
/**
* 密碼錯(cuò)誤次數(shù)
*/
@ApiModelProperty(notes = "密碼錯(cuò)誤次數(shù)")
@TableField(value = "password_error_num")
private Integer password_error_num;
/**
* 密碼過(guò)期時(shí)間
*/
@ApiModelProperty(notes = "密碼過(guò)期時(shí)間")
@TableField(value = "password_expire_time")
private Date password_expire_time;
/**
* 密碼
*/
@ApiModelProperty(notes = "密碼")
@TableField(value = "password")
private String password;
/**
* 最后登錄時(shí)間
*/
@ApiModelProperty(notes = "最后登錄時(shí)間")
@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)建時(shí)間
*/
@ApiModelProperty(notes = "創(chuàng)建時(shí)間")
@TableField(value = "create_time",fill = FieldFill.INSERT)
private Date create_time;
/**
* 更新人id
*/
@ApiModelProperty(notes = "更新人id")
@TableField(value = "update_user")
private Long update_user;
/**
* 更新時(shí)間
*/
@ApiModelProperty(notes = "更新時(shí)間")
@TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
private Date update_time;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}
swagger顯示
8、注解