配置接口注解和文档说明
This commit is contained in:
parent
0e1da3342e
commit
50cbb58fd9
26
README.md
26
README.md
@ -1,3 +1,27 @@
|
|||||||
# spring-boot-openapi
|
# spring-boot-openapi
|
||||||
|
|
||||||
springboot下的openapi功能测试,包含有Swagger和springdoc
|
> SpringBoot下的OpenApi功能测试,包含有Swagger和SpringDoc,对于SpringBoot下Swagger相关功能配置和授权相关,授权如 apikey,basic,oauth2,jwt等
|
||||||
|
|
||||||
|
|
||||||
|
项目环境
|
||||||
|
- jdk1.8
|
||||||
|
- Maven 3.8.1
|
||||||
|
- spring-boot 2.7.16
|
||||||
|
- springdoc-openapi-ui 1.7.0
|
||||||
|
- springfox-boot-starter 3.0.0
|
||||||
|
|
||||||
|
## 1. 项目结构
|
||||||
|
|
||||||
|
```cmd
|
||||||
|
├─spring-boot-model # 项目测试实体
|
||||||
|
├─spring-boot-springdoc # SpringBoot集成SpringDoc
|
||||||
|
└─spring-boot-swagger # SpringBoot集成SpringFox(这里使用的是最后的版本Swagger3.0,其中SwaggerConfig配置的是SWAGGER_2,SwaggerOpenApiConfig.java配置的是OAS_30)
|
||||||
|
```
|
||||||
|
## 2. 具体演示
|
||||||
|
|
||||||
|
- SpringFox Swagger
|
||||||
|

|
||||||
|
- Spring Doc
|
||||||
|

|
||||||
|
- oauth2
|
||||||
|

|
||||||
|
|||||||
BIN
img/Swagger.png
Normal file
BIN
img/Swagger.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
BIN
img/img.png
Normal file
BIN
img/img.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
BIN
img/oauth2.png
Normal file
BIN
img/oauth2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
7
pom.xml
7
pom.xml
@ -25,6 +25,7 @@
|
|||||||
<!--阿里的json工具依赖-->
|
<!--阿里的json工具依赖-->
|
||||||
<fastjson.version>2.0.32</fastjson.version>
|
<fastjson.version>2.0.32</fastjson.version>
|
||||||
<swagger.version>3.0.0</swagger.version>
|
<swagger.version>3.0.0</swagger.version>
|
||||||
|
<springdoc.version>1.7.0</springdoc.version>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
@ -42,6 +43,12 @@
|
|||||||
<version>${fastjson.version}</version>
|
<version>${fastjson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springdoc</groupId>
|
||||||
|
<artifactId>springdoc-openapi-ui</artifactId>
|
||||||
|
<version>${springdoc.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
<artifactId>springfox-boot-starter</artifactId>
|
<artifactId>springfox-boot-starter</artifactId>
|
||||||
|
|||||||
@ -28,7 +28,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springdoc</groupId>
|
<groupId>org.springdoc</groupId>
|
||||||
<artifactId>springdoc-openapi-ui</artifactId>
|
<artifactId>springdoc-openapi-ui</artifactId>
|
||||||
<version>1.7.0</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.jnssd.config;
|
package com.jnssd.config;
|
||||||
|
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
@ -39,9 +40,10 @@ public class SwaggerOpenApiConfig {
|
|||||||
.select()
|
.select()
|
||||||
// 扫描特定包
|
// 扫描特定包
|
||||||
// 扫描所有有注解的api,用这种方式更灵活
|
// 扫描所有有注解的api,用这种方式更灵活
|
||||||
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
||||||
//.apis(RequestHandlerSelectors.any())
|
//.apis(RequestHandlerSelectors.any())
|
||||||
.apis(RequestHandlerSelectors.basePackage("com.jnssd"))
|
// 扫描指定包下的
|
||||||
|
// .apis(RequestHandlerSelectors.basePackage("com.jnssd"))
|
||||||
.paths(PathSelectors.any())
|
.paths(PathSelectors.any())
|
||||||
.build()
|
.build()
|
||||||
.securitySchemes(initSecuritySchemeList())
|
.securitySchemes(initSecuritySchemeList())
|
||||||
@ -51,7 +53,7 @@ public class SwaggerOpenApiConfig {
|
|||||||
public ApiInfo apiInfo() {
|
public ApiInfo apiInfo() {
|
||||||
return new ApiInfoBuilder()
|
return new ApiInfoBuilder()
|
||||||
.title("Swagger项目测试")
|
.title("Swagger项目测试")
|
||||||
.description("novel项目接口文档")
|
.description("Swagger项目接口文档")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,14 +61,14 @@ public class SwaggerOpenApiConfig {
|
|||||||
|
|
||||||
List<SecurityScheme> list = new ArrayList<>();
|
List<SecurityScheme> list = new ArrayList<>();
|
||||||
list.add(httpAuthenticationScheme());
|
list.add(httpAuthenticationScheme());
|
||||||
// list.add(securitySchemeApiKey());
|
list.add(securitySchemeApiKey());
|
||||||
// list.add(securitySchemeOpenIdConnect());
|
list.add(securitySchemeOpenIdConnect());
|
||||||
//
|
|
||||||
// // 配置oauth2的几种模式
|
// 配置oauth2的几种模式
|
||||||
// list.add(securitySchemeOauth2ClientCredentials());
|
list.add(securitySchemeOauth2ClientCredentials());
|
||||||
// list.add(securitySchemeOauth2implicit());
|
list.add(securitySchemeOauth2implicit());
|
||||||
// list.add(securitySchemeOauth2Password());
|
list.add(securitySchemeOauth2Password());
|
||||||
// list.add(securitySchemeOauth2AuthorizationCode());
|
list.add(securitySchemeOauth2AuthorizationCode());
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
package com.jnssd.controller;
|
package com.jnssd.controller;
|
||||||
|
|
||||||
import com.jnssd.model.Menu;
|
import com.jnssd.model.Menu;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@ -20,6 +18,7 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/menu")
|
@RequestMapping("/menu")
|
||||||
|
@Api(tags = "菜单")
|
||||||
public class MenuController {
|
public class MenuController {
|
||||||
|
|
||||||
final static String SUCCESS_TEXT = "操作成功!";
|
final static String SUCCESS_TEXT = "操作成功!";
|
||||||
@ -28,12 +27,14 @@ public class MenuController {
|
|||||||
List<Menu> list = new java.util.ArrayList<>();
|
List<Menu> list = new java.util.ArrayList<>();
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
|
@ApiOperation(value = "获取所有菜单", notes = "获取所有菜单")
|
||||||
public ResponseEntity<List<Menu>> getAll() {
|
public ResponseEntity<List<Menu>> getAll() {
|
||||||
return ResponseEntity.ok(list);
|
return ResponseEntity.ok(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加方法
|
// 添加方法
|
||||||
@PostMapping("add")
|
@PostMapping("add")
|
||||||
|
@ApiOperation(value = "添加菜单", notes = "添加菜单")
|
||||||
public ResponseEntity<String> add(Menu entity) {
|
public ResponseEntity<String> add(Menu entity) {
|
||||||
try {
|
try {
|
||||||
entity.setId(list.size() + 1);
|
entity.setId(list.size() + 1);
|
||||||
@ -46,7 +47,8 @@ public class MenuController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 添加方法
|
// 添加方法
|
||||||
@PostMapping("update")
|
@PutMapping("update")
|
||||||
|
@ApiOperation(value = "修改菜单", notes = "修改菜单")
|
||||||
public ResponseEntity<Object> update(Menu entity) {
|
public ResponseEntity<Object> update(Menu entity) {
|
||||||
try {
|
try {
|
||||||
// 修改list下面的数据
|
// 修改list下面的数据
|
||||||
@ -59,7 +61,8 @@ public class MenuController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 删除方法
|
// 删除方法
|
||||||
@PostMapping("delete")
|
@DeleteMapping("delete")
|
||||||
|
@ApiOperation(value = "删除菜单", notes = "删除菜单")
|
||||||
public ResponseEntity<String> delete(Menu entity) {
|
public ResponseEntity<String> delete(Menu entity) {
|
||||||
try {
|
try {
|
||||||
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
|
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
|
||||||
@ -70,7 +73,8 @@ public class MenuController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 查询方法
|
// 查询方法
|
||||||
@PostMapping("query")
|
@GetMapping("query")
|
||||||
|
@ApiOperation(value = "查询菜单", notes = "查询菜单")
|
||||||
public ResponseEntity<Object> query(Integer id) {
|
public ResponseEntity<Object> query(Integer id) {
|
||||||
try {
|
try {
|
||||||
Menu menu = (Menu) list.stream().filter(e -> Objects.equals(e.getId(), id));
|
Menu menu = (Menu) list.stream().filter(e -> Objects.equals(e.getId(), id));
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
package com.jnssd.controller;
|
package com.jnssd.controller;
|
||||||
|
|
||||||
import com.jnssd.model.Role;
|
import com.jnssd.model.Role;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -18,7 +17,8 @@ import java.util.Objects;
|
|||||||
* @since 2023-10-12 16:32:38
|
* @since 2023-10-12 16:32:38
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/Role")
|
@RequestMapping("/role")
|
||||||
|
@Api(tags = "角色")
|
||||||
public class RoleController {
|
public class RoleController {
|
||||||
|
|
||||||
final static String SUCCESS_TEXT = "操作成功!";
|
final static String SUCCESS_TEXT = "操作成功!";
|
||||||
@ -27,12 +27,14 @@ public class RoleController {
|
|||||||
List<Role> list = new java.util.ArrayList<>();
|
List<Role> list = new java.util.ArrayList<>();
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
|
@ApiOperation(value = "获取所有角色", notes = "获取所有角色")
|
||||||
public ResponseEntity<List<Role>> getAll() {
|
public ResponseEntity<List<Role>> getAll() {
|
||||||
return ResponseEntity.ok(list);
|
return ResponseEntity.ok(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加方法
|
// 添加方法
|
||||||
@PostMapping("add")
|
@PostMapping("add")
|
||||||
|
@ApiOperation(value = "添加角色", notes = "添加角色")
|
||||||
public ResponseEntity<String> add(Role entity) {
|
public ResponseEntity<String> add(Role entity) {
|
||||||
try {
|
try {
|
||||||
entity.setId(list.size() + 1);
|
entity.setId(list.size() + 1);
|
||||||
@ -45,7 +47,8 @@ public class RoleController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 添加方法
|
// 添加方法
|
||||||
@PostMapping("update")
|
@PutMapping("update")
|
||||||
|
@ApiOperation(value = "修改角色", notes = "修改角色")
|
||||||
public ResponseEntity<Object> update(Role entity) {
|
public ResponseEntity<Object> update(Role entity) {
|
||||||
try {
|
try {
|
||||||
// 修改list下面的数据
|
// 修改list下面的数据
|
||||||
@ -58,7 +61,8 @@ public class RoleController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 删除方法
|
// 删除方法
|
||||||
@PostMapping("delete")
|
@DeleteMapping("delete")
|
||||||
|
@ApiOperation(value = "删除角色", notes = "删除角色")
|
||||||
public ResponseEntity<String> delete(Role entity) {
|
public ResponseEntity<String> delete(Role entity) {
|
||||||
try {
|
try {
|
||||||
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
|
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
|
||||||
@ -69,7 +73,8 @@ public class RoleController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 查询方法
|
// 查询方法
|
||||||
@PostMapping("query")
|
@GetMapping("query")
|
||||||
|
@ApiOperation(value = "查询角色", notes = "查询角色")
|
||||||
public ResponseEntity<Object> query(Integer id) {
|
public ResponseEntity<Object> query(Integer id) {
|
||||||
try {
|
try {
|
||||||
Role role = (Role) list.stream().filter(e -> Objects.equals(e.getId(), id));
|
Role role = (Role) list.stream().filter(e -> Objects.equals(e.getId(), id));
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
package com.jnssd.controller;
|
package com.jnssd.controller;
|
||||||
|
|
||||||
import com.jnssd.model.User;
|
import com.jnssd.model.User;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -19,6 +18,7 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/user")
|
||||||
|
@Api(tags = "用户")
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
final static String SUCCESS_TEXT = "操作成功!";
|
final static String SUCCESS_TEXT = "操作成功!";
|
||||||
@ -27,12 +27,14 @@ public class UserController {
|
|||||||
List<User> list = new java.util.ArrayList<>();
|
List<User> list = new java.util.ArrayList<>();
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
|
@ApiOperation(value = "获取所有用户", notes = "获取所有用户")
|
||||||
public ResponseEntity<List<User>> getAll() {
|
public ResponseEntity<List<User>> getAll() {
|
||||||
return ResponseEntity.ok(list);
|
return ResponseEntity.ok(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加方法
|
// 添加方法
|
||||||
@PostMapping("add")
|
@PostMapping("add")
|
||||||
|
@ApiOperation(value = "添加用户", notes = "添加用户")
|
||||||
public ResponseEntity<String> add(User entity) {
|
public ResponseEntity<String> add(User entity) {
|
||||||
try {
|
try {
|
||||||
entity.setId(list.size() + 1);
|
entity.setId(list.size() + 1);
|
||||||
@ -45,7 +47,8 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 添加方法
|
// 添加方法
|
||||||
@PostMapping("update")
|
@PutMapping("update")
|
||||||
|
@ApiOperation(value = "修改用户", notes = "修改用户")
|
||||||
public ResponseEntity<Object> update(User entity) {
|
public ResponseEntity<Object> update(User entity) {
|
||||||
try {
|
try {
|
||||||
// 修改list下面的数据
|
// 修改list下面的数据
|
||||||
@ -58,7 +61,8 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 删除方法
|
// 删除方法
|
||||||
@PostMapping("delete")
|
@DeleteMapping("delete")
|
||||||
|
@ApiOperation(value = "删除用户", notes = "删除用户")
|
||||||
public ResponseEntity<String> delete(User entity) {
|
public ResponseEntity<String> delete(User entity) {
|
||||||
try {
|
try {
|
||||||
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
|
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
|
||||||
@ -69,7 +73,8 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 查询方法
|
// 查询方法
|
||||||
@PostMapping("query")
|
@GetMapping("query")
|
||||||
|
@ApiOperation(value = "查询用户", notes = "查询用户")
|
||||||
public ResponseEntity<Object> query(Integer id) {
|
public ResponseEntity<Object> query(Integer id) {
|
||||||
try {
|
try {
|
||||||
User user = (User) list.stream().filter(e -> Objects.equals(e.getId(), id));
|
User user = (User) list.stream().filter(e -> Objects.equals(e.getId(), id));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user