From 511785edf0d58807aeb474813f44c2161d0b7fbe Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Sun, 5 Mar 2023 23:18:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E5=9F=BA=E4=BA=8EOpenApi3=E5=B9=B6?= =?UTF-8?q?=E9=9B=86=E6=88=90SpringDoc=E5=92=8CKnife4j?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- swagger_study/pom.xml | 25 ++-- .../bnblogs/swagger_study/config/DocInfo.java | 22 ++++ .../swagger_study/config/SpringDocConfig.java | 57 ++++++++++ .../swagger_study/config/SwaggerConfig.java | 107 +++++++----------- .../controller/DemoController.java | 21 ++-- .../controller/HelloController.java | 1 + .../bnblogs/swagger_study/pojo/Student.java | 10 +- .../src/main/resources/application.yml | 14 ++- 8 files changed, 162 insertions(+), 95 deletions(-) create mode 100644 swagger_study/src/main/java/cc/bnblogs/swagger_study/config/DocInfo.java create mode 100644 swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SpringDocConfig.java diff --git a/swagger_study/pom.xml b/swagger_study/pom.xml index 7671587..e844dfd 100644 --- a/swagger_study/pom.xml +++ b/swagger_study/pom.xml @@ -23,30 +23,25 @@ - io.springfox - springfox-swagger2 - 2.9.2 - - - io.swagger - swagger-models - - + org.springframework.boot + spring-boot-configuration-processor + true - io.swagger - swagger-models - 1.5.22 + com.github.xiaoymin + knife4j-springdoc-ui + 3.0.3 - io.springfox - springfox-swagger-ui - 2.9.2 + org.springdoc + springdoc-openapi-ui + 1.6.14 + org.springframework.boot spring-boot-devtools diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/DocInfo.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/DocInfo.java new file mode 100644 index 0000000..a2a998a --- /dev/null +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/DocInfo.java @@ -0,0 +1,22 @@ +package cc.bnblogs.swagger_study.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * @description: + * @author: zfp@bnblogs.cc + * @date: 2023/3/5 21:15 + */ +@Data +@Component +@ConfigurationProperties(prefix = "doc-info") +public class DocInfo { + + private String title = "SpringBoot集成SpringDoc"; + private String description = "简单使用入门"; + private String version = "1.0"; + private String websiteName = "blog"; + private String websiteUrl = "https://hugo.bnblogs.cc"; +} \ No newline at end of file diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SpringDocConfig.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SpringDocConfig.java new file mode 100644 index 0000000..d49544b --- /dev/null +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SpringDocConfig.java @@ -0,0 +1,57 @@ +package cc.bnblogs.swagger_study.config; + + +import io.swagger.v3.oas.models.ExternalDocumentation; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import org.springdoc.core.GroupedOpenApi; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +/** + * @description: SpringDoc配置 + * @author: zfp@bnblogs.cc + * @date: 2023/3/5 20:25 + */ +@Configuration +public class SpringDocConfig { + @Autowired + private DocInfo docInfo; + @Bean + public OpenAPI myOpenAPI() { + return new OpenAPI() + .info(new Info().title(docInfo.getTitle()) + .description(docInfo.getDescription()) + .version(docInfo.getVersion())) + .externalDocs(new ExternalDocumentation().description(docInfo.getWebsiteName()) + .url(docInfo.getWebsiteUrl())); + } + + + @Bean + public GroupedOpenApi allApi() { + return GroupedOpenApi.builder() + .group("all") + .pathsToMatch("/**") + .build(); + } + + @Bean + public GroupedOpenApi DemoApi() { + return GroupedOpenApi.builder() + .group("demo") + .pathsToMatch("/demo/**") + .build(); + } + + @Bean + public GroupedOpenApi helloApi() { + return GroupedOpenApi.builder() + .group("hello") + .pathsToMatch("/hello/**") + .build(); + } + +} \ No newline at end of file diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java index ce19a1d..993c5f7 100644 --- a/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/config/SwaggerConfig.java @@ -1,24 +1,5 @@ package cc.bnblogs.swagger_study.config; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.Environment; -import org.springframework.core.env.Profiles; -import springfox.documentation.builders.ParameterBuilder; -import springfox.documentation.schema.ModelRef; -import springfox.documentation.service.Parameter; -import springfox.documentation.swagger2.annotations.EnableSwagger2; -import org.springframework.context.annotation.Bean; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.Contact; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; - -import java.util.ArrayList; -import java.util.List; /** @@ -26,50 +7,50 @@ import java.util.List; * @author: zfp@bnblogs.cc * @date: 2023/3/5 0:00 */ - -@EnableSwagger2 -@Configuration -public class SwaggerConfig { - @Bean - public Docket docket(){ - // 新建一个全局参数 - Parameter token = new ParameterBuilder().name("token") - // 描述信息 - .description("用户登录令牌") - // 该参数放在请求头 - .parameterType("header") // 也可以设置query参数,相当于@RequestParam - // 参数的类型 - .modelRef(new ModelRef("String")) - // 该参数必填 - .required(true) - .build(); - - List parameters = new ArrayList<>(); - parameters.add(token); - - return new Docket(DocumentationType.SWAGGER_2) - .globalOperationParameters(parameters).apiInfo(apiInfo()); - - } - +// +//@EnableSwagger2 +//@Configuration +//public class SwaggerConfig { // @Bean -// public Docket RestfulApi() { +// public Docket docket(){ +// // 新建一个全局参数 +// Parameter token = new ParameterBuilder().name("token") +// // 描述信息 +// .description("用户登录令牌") +// // 该参数放在请求头 +// .parameterType("header") // 也可以设置query参数,相当于@RequestParam +// // 参数的类型 +// .modelRef(new ModelRef("String")) +// // 该参数必填 +// .required(true) +// .build(); +// +// List parameters = new ArrayList<>(); +// parameters.add(token); +// // return new Docket(DocumentationType.SWAGGER_2) -// .apiInfo(apiInfo()) -// .select() -// .apis(RequestHandlerSelectors.basePackage("cc.bnblogs.swagger_study.controller")) -// .paths(PathSelectors.any()).build(); +// .globalOperationParameters(parameters).apiInfo(apiInfo()); +// +// } +// +//// @Bean +//// public Docket RestfulApi() { +//// return new Docket(DocumentationType.SWAGGER_2) +//// .apiInfo(apiInfo()) +//// .select() +//// .apis(RequestHandlerSelectors.basePackage("cc.bnblogs.swagger_study.controller")) +//// .paths(PathSelectors.any()).build(); +//// } +// private ApiInfo apiInfo() { +// return new ApiInfoBuilder() +// //页面标题 +// .title("Spring Boot集成Swagger2构建RESTful API") +// //创建人 +// .contact(new Contact("barney", "https://hugo.bnblogs.cc", "tzhangfp@163.com")) +// //版本号 +// .version("1.0") +// //描述 +// .description("学习使用Swagger") +// .build(); // } - private ApiInfo apiInfo() { - return new ApiInfoBuilder() - //页面标题 - .title("Spring Boot集成Swagger2构建RESTful API") - //创建人 - .contact(new Contact("barney", "https://hugo.bnblogs.cc", "tzhangfp@163.com")) - //版本号 - .version("1.0") - //描述 - .description("学习使用Swagger") - .build(); - } -} +//} diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java index e8319a0..849e37d 100644 --- a/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/DemoController.java @@ -1,28 +1,27 @@ package cc.bnblogs.swagger_study.controller; import cc.bnblogs.swagger_study.pojo.Student; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiImplicitParam; -import io.swagger.annotations.ApiImplicitParams; -import io.swagger.annotations.ApiOperation; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.*; -@Api(tags = "demo接口") +@Tag(name = "demo接口列表") @RestController @RequestMapping("/demo/") public class DemoController { @GetMapping - @ApiOperation("输出hello world") + @Operation(summary = "打印hello world") public String greet() { return "Hello, World"; } - @PostMapping("/{id}/") - @ApiOperation("提交一个id") - @ApiImplicitParams({ - @ApiImplicitParam(name = "id", value = "编号", example = "1"), - @ApiImplicitParam(name = "name", value = "姓名", example = "admin") + @Parameters(value={ + @Parameter(name = "id",description = "编号",example = "1"), + @Parameter(name = "name",description = "姓名",example = "admin"), }) + @PostMapping("/{id}/") public Integer addInfo(@RequestParam Integer id, @RequestParam String name) { System.out.println(name); diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java index be44ffd..d343bc1 100644 --- a/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/controller/HelloController.java @@ -1,5 +1,6 @@ package cc.bnblogs.swagger_study.controller; +import io.swagger.v3.oas.annotations.Parameter; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; diff --git a/swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java b/swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java index f59febb..bb1cc5f 100644 --- a/swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java +++ b/swagger_study/src/main/java/cc/bnblogs/swagger_study/pojo/Student.java @@ -1,7 +1,7 @@ package cc.bnblogs.swagger_study.pojo; -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; + +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -14,11 +14,11 @@ import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor -@ApiModel("学生表") +@Schema(title = "学生表") public class Student { - @ApiModelProperty("学号") + @Schema(title = "学号") private Integer id; - @ApiModelProperty("姓名") + @Schema(title = "姓名") private String name; } diff --git a/swagger_study/src/main/resources/application.yml b/swagger_study/src/main/resources/application.yml index 1e42909..9167958 100644 --- a/swagger_study/src/main/resources/application.yml +++ b/swagger_study/src/main/resources/application.yml @@ -2,4 +2,16 @@ # profiles: # active: test # 当前环境为dev server: - port: 8080 \ No newline at end of file + port: 8080 + +springdoc: + packages-to-scan: cc.bnblogs.swagger_study.controller + swagger-ui: + enabled: true + +doc-info: + title: SpringDoc使用 + description: 基于Spring Boot 2.7.9 + version: 1.2 + website-name: bnblogs + website-url: https://bnblogs.cc \ No newline at end of file