This commit is contained in:
2023-10-13 17:41:41 +08:00
commit af09dd3cf3
20 changed files with 944 additions and 0 deletions

33
.gitignore vendored Normal file
View File

@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# spring-boot-openapi
springboot下的openapi功能测试包含有Swagger和springdoc

98
pom.xml Normal file
View File

@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.16</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jnssd</groupId>
<artifactId>spring-boot-openapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>spring-boot-openapi</name>
<description>spring-boot-openapi</description>
<modules>
<module>spring-boot-swagger</module>
<module>spring-boot-springdoc</module>
<module>spring-boot-model</module>
</modules>
<properties>
<java.version>1.8</java.version>
<!--阿里的json工具依赖-->
<fastjson.version>2.0.32</fastjson.version>
<swagger.version>3.0.0</swagger.version>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.jnssd</groupId>
<artifactId>spring-boot-model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

20
spring-boot-model/pom.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jnssd</groupId>
<artifactId>spring-boot-openapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-model</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,28 @@
package com.jnssd.model;
import lombok.Data;
/**
* <h3>spring-boot-openapi</h3>
* <p>菜单</p>
*
* @author zxj
* @since 2023-10-12 16:33:23
*/
@Data
public class Menu {
private Integer id;
private String name;
private String url;
private String icon;
private String parentId;
private String parentName;
private String parentUrl;
private String parentIcon;
private String description;
private String sort;
private String type;
private String status;
private String createTime;
private String updateTime;
}

View File

@ -0,0 +1,18 @@
package com.jnssd.model;
import lombok.Data;
/**
* <h3>spring-boot-openapi</h3>
* <p>角色</p>
*
* @author zxj
* @since 2023-10-12 16:32:38
*/
@Data
public class Role {
private Integer id;
private String name;
private String description;
private String createTime;
}

View File

@ -0,0 +1,26 @@
package com.jnssd.model;
import lombok.Data;
/**
* <h3>spring-boot-openapi</h3>
* <p>用户</p>
*
* @author zxj
* @since 2023-10-12 16:30:04
*/
@Data
public class User {
private Integer id;
private String name;
private String password;
private String email;
private String phone;
private String address;
private String description;
private String createTime;
private String updateTime;
private String status;
}

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jnssd</groupId>
<artifactId>spring-boot-openapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-springdoc</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.jnssd</groupId>
<artifactId>spring-boot-model</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,52 @@
package com.jnssd.controller;
import com.jnssd.model.Menu;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <h3>spring-boot-openapi</h3>
* <p>菜单</p>
*
* @author zxj
* @since 2023-10-12 16:33:23
*/
@RestController("menu")
public class MenuController {
List<Menu> list = new java.util.ArrayList<>();
public ResponseEntity<List<Menu>> getAll() {
return ResponseEntity.ok(list);
}
// 添加方法
@PostMapping("add")
public ResponseEntity<Menu> add(Menu entity) {
list.add(entity);
return ResponseEntity.ok(entity);
}
// 添加方法
@PostMapping("update")
public ResponseEntity<Menu> update(Menu entity) {
return null;
}
// 删除方法
@PostMapping("delete")
public ResponseEntity<Menu> delete(Menu entity) {
return null;
}
// 查询方法
@PostMapping("query")
public ResponseEntity<List<Menu>> query(Menu entity) {
return null;
}
}

View File

@ -0,0 +1,18 @@
package com.jnssd.controller;
import lombok.Data;
/**
* <h3>spring-boot-openapi</h3>
* <p>角色</p>
*
* @author zxj
* @since 2023-10-12 16:32:38
*/
@Data
public class RoleController {
private String id;
private String name;
private String description;
private String createTime;
}

View File

@ -0,0 +1,26 @@
package com.jnssd.controller;
import lombok.Data;
/**
* <h3>spring-boot-openapi</h3>
* <p>用户</p>
*
* @author zxj
* @since 2023-10-12 16:30:04
*/
@Data
public class UserController {
private String id;
private String name;
private String password;
private String email;
private String phone;
private String address;
private String description;
private String createTime;
private String updateTime;
private String status;
}

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jnssd</groupId>
<artifactId>spring-boot-openapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-swagger</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.jnssd</groupId>
<artifactId>spring-boot-model</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
package com.jnssd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}

View File

@ -0,0 +1,128 @@
// package com.jnssd.config;
//
// import io.swagger.annotations.ApiOperation;
// import org.springframework.context.annotation.Bean;
// import org.springframework.context.annotation.Configuration;
// import springfox.documentation.builders.ApiInfoBuilder;
// import springfox.documentation.builders.OAuthBuilder;
// import springfox.documentation.builders.PathSelectors;
// import springfox.documentation.builders.RequestHandlerSelectors;
// import springfox.documentation.oas.annotations.EnableOpenApi;
// import springfox.documentation.service.*;
// import springfox.documentation.spi.DocumentationType;
// import springfox.documentation.spi.service.contexts.SecurityContext;
// import springfox.documentation.spring.web.plugins.Docket;
// import springfox.documentation.swagger.web.ApiKeyVehicle;
// import springfox.documentation.swagger.web.SecurityConfiguration;
// import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
// import springfox.documentation.swagger2.annotations.EnableSwagger2;
//
// import java.util.ArrayList;
// import java.util.Collections;
// import java.util.List;
//
// /**
// * <h3>spring-boot-openapi</h3>
// * <p></p>
// *
// * @author zxj
// * @since 2023-10-12 17:13:09
// */
// @Configuration
// @EnableSwagger2
// @EnableOpenApi
// public class SwaggerConfig {
//
// @Bean
// public Docket api() {
// return new Docket(DocumentationType.SWAGGER_2)
// .apiInfo(apiInfo())
// .select()
// // 扫描特定包
// // 扫描所有有注解的api用这种方式更灵活
// // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// //.apis(RequestHandlerSelectors.any())
// .apis(RequestHandlerSelectors.basePackage("com.jnssd"))
// .paths(PathSelectors.any())
// .build()
// .securitySchemes(initSecuritySchemeList())
// .securityContexts(Collections.singletonList(securityContext()));
// }
// public ApiInfo apiInfo() {
// return new ApiInfoBuilder()
// .title("Swagger项目测试")
// .description("novel项目接口文档")
// .build();
// }
//
// private SecurityContext securityContext() {
// return SecurityContext.builder()
// .securityReferences(Collections.singletonList(new SecurityReference("OAuth2", new AuthorizationScope[0])))
// .build();
// }
//
// private List<AuthorizationScope> scopes() {
// List<AuthorizationScope> list = new ArrayList<>();
// list.add(new AuthorizationScope("read_scope", "Grants read access"));
// list.add(new AuthorizationScope("write_scope", "Grants write access"));
// list.add(new AuthorizationScope("admin_scope", "Grants read write and delete access"));
// return list;
// }
//
// public List<SecurityScheme> initSecuritySchemeList() {
// List<SecurityScheme> list = new ArrayList<>();
// list.add(securitySchemeOAuth2());
// list.add(securitySchemeApiKey());
// // list.add(securitySchemeHttp());
// list.add(securitySchemeBasicAuth());
// list.add(securitySchemeOauth2Schema());
// return list;
// }
//
// private SecurityScheme securitySchemeOAuth2() {
// List<GrantType> grantTypes = new ArrayList<>();
// grantTypes.add(new ResourceOwnerPasswordCredentialsGrant("/oauth/token"));
// return new OAuthBuilder()
// .name("OAuth2")
// .scopes(scopes())
// .grantTypes(grantTypes)
// .build();
// }
//
// private SecurityScheme securitySchemeApiKey() {
// return new ApiKey("Authorization", "api_key", ApiKeyVehicle.HEADER.getValue());
// }
//
// private SecurityScheme securitySchemeHttp() {
// return HttpAuthenticationScheme.JWT_BEARER_BUILDER.name("JWT").build();
// }
//
// private SecurityScheme securitySchemeBasicAuth() {
// return new BasicAuth("basicAuth");
// }
//
// private SecurityScheme securitySchemeOauth2Schema() {
// List<GrantType> grantTypes = new ArrayList<>();
// TokenRequestEndpoint tokenRequestEndpoint = new TokenRequestEndpoint("/oauth/authorize", "", "");
// TokenEndpoint tokenEndpoint = new TokenEndpoint("/oauth/token", "token");
// grantTypes.add(new AuthorizationCodeGrant(tokenRequestEndpoint, tokenEndpoint));
// return new OAuthBuilder()
// .name("oauth2schema")
// .scopes(scopes())
// .grantTypes(grantTypes)
// .build();
// }
//
// @Bean
// public SecurityConfiguration security() {
// return SecurityConfigurationBuilder.builder()
// .clientId("")
// .clientSecret("")
// .realm("*")
// .appName("")
// .scopeSeparator("")
// .useBasicAuthenticationWithAccessCodeGrant(false)
// .build();
// }
//
// }

View File

@ -0,0 +1,145 @@
package com.jnssd.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* <h3>spring-boot-openapi</h3>
* <p>配置openapi</p>
*
* @author zxj
* @since 2023-10-13 14:29:58
*/
@Configuration
@EnableOpenApi
public class SwaggerOpenApiConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
// 扫描特定包
// 扫描所有有注解的api用这种方式更灵活
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//.apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.jnssd"))
.paths(PathSelectors.any())
.build()
.securitySchemes(initSecuritySchemeList())
.securityContexts(Collections.singletonList(securityContext()));
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger项目测试")
.description("novel项目接口文档")
.build();
}
private List<SecurityScheme> initSecuritySchemeList() {
List<SecurityScheme> list = new ArrayList<>();
list.add(httpAuthenticationScheme());
list.add(securitySchemeApiKey());
list.add(securitySchemeOpenIdConnect());
// 配置oauth2的几种模式
list.add(securitySchemeOauth2ClientCredentials());
list.add(securitySchemeOauth2implicit());
list.add(securitySchemeOauth2Password());
list.add(securitySchemeOauth2AuthorizationCode());
return list;
}
private SecurityScheme httpAuthenticationScheme() {
return HttpAuthenticationScheme.JWT_BEARER_BUILDER.name("JWT的值").build();
}
// ApiKey模式
private SecurityScheme securitySchemeApiKey() {
return new ApiKey("Authorization授权", "Authorization", "header");
}
// OpenIdConnect
private SecurityScheme securitySchemeOpenIdConnect() {
return new OpenIdConnectSchemeBuilder()
.name("OpenId授权")
.description("OpenIdConnect授权配置")
.openIdConnectUrl("/oauth/authorize")
.build();
}
// 客户端模式
private SecurityScheme securitySchemeOauth2ClientCredentials() {
return OAuth2Scheme.OAUTH2_CLIENT_CREDENTIALS_FLOW_BUILDER
.name("客户端模式")
.tokenUrl("/oauth/authorize")
.scopes(scopes())
.build();
}
// 隐式模式
private SecurityScheme securitySchemeOauth2implicit() {
return OAuth2Scheme.OAUTH2_IMPLICIT_FLOW_BUILDER
.name("简化模式")
.authorizationUrl("/oauth/authorize")
.scopes(scopes())
.build();
}
// 密码模式
private SecurityScheme securitySchemeOauth2Password() {
return OAuth2Scheme.OAUTH2_PASSWORD_FLOW_BUILDER
.name("密码模式")
.tokenUrl("/oauth/token")
.scopes(scopes())
.build();
}
// 授权码模式
private SecurityScheme securitySchemeOauth2AuthorizationCode() {
return OAuth2Scheme.OAUTH2_AUTHORIZATION_CODE_FLOW_BUILDER
.name("授权码模式")
.authorizationUrl("/oauth/authorize")
.tokenUrl("/oauth/token")
.scopes(scopes())
.build();
}
private List<AuthorizationScope> scopes() {
List<AuthorizationScope> list = new ArrayList<>();
list.add(new AuthorizationScope("read_scope", "Grants read access"));
list.add(new AuthorizationScope("write_scope", "Grants write access"));
list.add(new AuthorizationScope("admin_scope", "Grants read write and delete access"));
return list;
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.operationSelector(operationContext -> {
System.out.println("operationContext" + operationContext);
return !operationContext.httpMethod().name().equals("GET");
})
.build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(new SecurityReference("密码模式", authorizationScopes));
}
}

View File

@ -0,0 +1,19 @@
package com.jnssd.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* <h3>spring-boot-openapi</h3>
* <p></p>
*
* @author zxj
* @since 2023-10-12 17:35:26
*/
@Controller
public class CommonController {
@GetMapping("/")
public String index() {
return "redirect:/swagger-ui/index.html";
}
}

View File

@ -0,0 +1,83 @@
package com.jnssd.controller;
import com.jnssd.model.Menu;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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.Objects;
/**
* <h3>spring-boot-openapi</h3>
* <p>菜单</p>
*
* @author zxj
* @since 2023-10-12 16:33:23
*/
@RestController
@RequestMapping("/menu")
public class MenuController {
final static String SUCCESS_TEXT = "操作成功!";
final static String FAIL_TEXT = "操作失败!";
List<Menu> list = new java.util.ArrayList<>();
@GetMapping("/")
public ResponseEntity<List<Menu>> getAll() {
return ResponseEntity.ok(list);
}
// 添加方法
@PostMapping("add")
public ResponseEntity<String> add(Menu entity) {
try {
entity.setId(list.size() + 1);
list.add(entity);
return ResponseEntity.ok(SUCCESS_TEXT);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
// 添加方法
@PostMapping("update")
public ResponseEntity<Object> update(Menu entity) {
try {
// 修改list下面的数据
list.replaceAll((menu) -> Objects.equals(menu.getId(), entity.getId()) ? entity : menu);
return ResponseEntity.ok(SUCCESS_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
// 删除方法
@PostMapping("delete")
public ResponseEntity<String> delete(Menu entity) {
try {
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
return result ? ResponseEntity.ok(SUCCESS_TEXT) : ResponseEntity.status(400).body(FAIL_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(null);
}
}
// 查询方法
@PostMapping("query")
public ResponseEntity<Object> query(Integer id) {
try {
Menu menu = (Menu) list.stream().filter(e -> Objects.equals(e.getId(), id));
// list 下查询
return ResponseEntity.ok(menu);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
}

View File

@ -0,0 +1,79 @@
package com.jnssd.controller;
import com.jnssd.model.Menu;
import com.jnssd.model.Role;
import lombok.Data;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
/**
* <h3>spring-boot-openapi</h3>
* <p>角色</p>
*
* @author zxj
* @since 2023-10-12 16:32:38
*/
@Data
public class RoleController {
final static String SUCCESS_TEXT = "操作成功!";
final static String FAIL_TEXT = "操作失败!";
List<Role> list = new java.util.ArrayList<>();
public ResponseEntity<List<Role>> getAll() {
return ResponseEntity.ok(list);
}
// 添加方法
@PostMapping("add")
public ResponseEntity<String> add(Role entity) {
try {
entity.setId(Objects.requireNonNull(list.stream().max(Comparator.comparingInt(Role::getId)).get()).getId() + 1);
list.add(entity);
return ResponseEntity.ok(SUCCESS_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
// 添加方法
@PostMapping("update")
public ResponseEntity<Object> update(Role entity) {
try {
// 修改list下面的数据
list.replaceAll((Role) -> Objects.equals(Role.getId(), entity.getId()) ? entity : Role);
return ResponseEntity.ok(SUCCESS_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
// 删除方法
@PostMapping("delete")
public ResponseEntity<String> delete(Role entity) {
try {
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
return result ? ResponseEntity.ok(SUCCESS_TEXT) : ResponseEntity.status(400).body(FAIL_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(null);
}
}
// 查询方法
@PostMapping("query")
public ResponseEntity<Object> query(Integer id) {
try {
Role Role = (Role) list.stream().filter(e -> Objects.equals(e.getId(), id));
// list 下查询
return ResponseEntity.ok(Role);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
}

View File

@ -0,0 +1,78 @@
package com.jnssd.controller;
import com.jnssd.model.User;
import lombok.Data;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
/**
* <h3>spring-boot-openapi</h3>
* <p>用户</p>
*
* @author zxj
* @since 2023-10-12 16:30:04
*/
@Data
public class UserController {
final static String SUCCESS_TEXT = "操作成功!";
final static String FAIL_TEXT = "操作失败!";
List<User> list = new java.util.ArrayList<>();
public ResponseEntity<List<User>> getAll() {
return ResponseEntity.ok(list);
}
// 添加方法
@PostMapping("add")
public ResponseEntity<String> add(User entity) {
try {
entity.setId(Objects.requireNonNull(list.stream().max(Comparator.comparingInt(User::getId)).get()).getId() + 1);
list.add(entity);
return ResponseEntity.ok(SUCCESS_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
// 添加方法
@PostMapping("update")
public ResponseEntity<Object> update(User entity) {
try {
// 修改list下面的数据
list.replaceAll((User) -> Objects.equals(User.getId(), entity.getId()) ? entity : User);
return ResponseEntity.ok(SUCCESS_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
// 删除方法
@PostMapping("delete")
public ResponseEntity<String> delete(User entity) {
try {
boolean result = list.removeIf(e -> Objects.equals(e.getId(), entity.getId()));
return result ? ResponseEntity.ok(SUCCESS_TEXT) : ResponseEntity.status(400).body(FAIL_TEXT);
} catch (Exception e) {
return ResponseEntity.status(400).body(null);
}
}
// 查询方法
@PostMapping("query")
public ResponseEntity<Object> query(Integer id) {
try {
User User = (User) list.stream().filter(e -> Objects.equals(e.getId(), id));
// list 下查询
return ResponseEntity.ok(User);
} catch (Exception e) {
return ResponseEntity.status(400).body(FAIL_TEXT);
}
}
}

View File

@ -0,0 +1,10 @@
server:
port: 18080
# ========================================================================
# 启动报错需要修改以下mvc配置 Failed to start bean 'documentationPluginsBootstrapper'
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
# ========================================================================