当前位置: 首页 > news >正文

做一个主题wordpress微软优化大师

做一个主题wordpress,微软优化大师,米方科技网站建设,关于asp.net的网站模板Spring Boot 3 整合 springdoc-openapi 概述 springdoc-openapi 是一个用于自动生成 OpenAPI 3.0 文档的库,它支持与 Spring Boot 无缝集成。通过这个库,你可以轻松地生成和展示 RESTful API 的文档,并且可以使用 Swagger UI 或 ReDoc 进行…

Spring Boot 3 整合 springdoc-openapi

概述

springdoc-openapi 是一个用于自动生成 OpenAPI 3.0 文档的库,它支持与 Spring Boot 无缝集成。通过这个库,你可以轻松地生成和展示 RESTful API 的文档,并且可以使用 Swagger UI 或 ReDoc 进行交互式测试。

环境准备

  • Spring Boot 3.x
  • Java 17+
  • Maven

创建 Spring Boot 项目

首先,创建一个新的 Spring Boot 项目。你可以使用 Spring Initializr 来快速生成项目结构。

添加依赖

pom.xml 文件中添加 springdoc-openapi-ui 依赖:

<dependencies><!-- Spring Web 依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- springdoc-openapi-starter-webmvc-ui 是一个 Spring Boot Starter,它包含了 springdoc-openapi-ui 及其他必要的依赖,简化了依赖管理和配置 --><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.6.0</version></dependency><!-- springdoc-openapi-ui 依赖 -->
<!--	<dependency>-->
<!--       <groupId>org.springdoc</groupId>-->
<!--       <artifactId>springdoc-openapi-ui</artifactId>-->
<!--       <version>1.8.0</version>-->
<!--    </dependency>-->
</dependencies>

配置文件

application.ymlapplication.properties 中配置 Swagger UI 和 ReDoc 的路径(可选):

springdoc:api-docs:path: /v3/api-docsswagger-ui:path: /swagger-ui.htmlenabled: trueoperationsSorter: methodshow-actuator: true

或者在 application.properties 中:

springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.swagger-ui.enabled=true
springdoc.swagger-ui.operations-sorter=method
springdoc.show-actuator=true

创建模型类

创建一个简单的模型类 User,并使用 @Schema 注解来描述字段:

package org.springdoc.demo.services.user.model;import io.swagger.v3.oas.annotations.media.Schema;@Schema(name = "User", description = "用户实体")
public class User {@Schema(description = "用户ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)private Long id;@Schema(description = "用户名", example = "john_doe", requiredMode = Schema.RequiredMode.REQUIRED)private String username;@Schema(description = "电子邮件", example = "john.doe@example.com", requiredMode = Schema.RequiredMode.REQUIRED)private String email;public User(Long id, String username, String email) {this.id = id;this.username = username;this.email = email;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}
}

如何想隐藏模型的某个字段,不生成文档,可以使用@Schema(hidden = true)。
当我们的 model 包含 JSR-303 Bean 验证注解(如 @NotNull、@NotBlank、@Size、@Min 和 @Max)时,springdoc-openapi 库会使用它们为相应的约束生成额外的 schema 文档。

/***  * Copyright 2019-2020 the original author or authors.*  **  * Licensed under the Apache License, Version 2.0 (the "License");*  * you may not use this file except in compliance with the License.*  * You may obtain a copy of the License at*  **  *      https://www.apache.org/licenses/LICENSE-2.0*  **  * Unless required by applicable law or agreed to in writing, software*  * distributed under the License is distributed on an "AS IS" BASIS,*  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*  * See the License for the specific language governing permissions and*  * limitations under the License.**/package org.springdoc.demo.services.book.model;import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;/*** The type Book.*/
public class Book {/*** The Id.*/@Schema(hidden = true)private long id;/*** The Title.*/@NotBlank@Size(min = 0, max = 20)private String title;/*** The Author.*/@NotBlank@Size(min = 0, max = 30)private String author;
}

创建 RESTful 控制器

创建一个控制器 UserController,包含两个方法:一个使用 OpenAPI 注解,另一个不使用。
我们使用 @Operation 和 @ApiResponses 对 controller 的 /api/user/{id} 端点进行注解。 其实不使用
@Operation 和 @ApiResponses,也会生成文档,只是信息少一些。

package org.springdoc.demo.services.user.controller;import org.springdoc.demo.services.user.model.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/api/user")
public class UserController {private final Map<Long, User> userMap = new HashMap<>();public UserController() {// 初始化一些示例数据userMap.put(1L, new User(1L, "john_doe", "john.doe@example.com"));userMap.put(2L, new User(2L, "jane_doe", "jane.doe@example.com"));}@Operation(summary = "获取用户信息",description = "根据用户ID获取用户信息",responses = {@ApiResponse(responseCode = "200",description = "成功",content = @Content(mediaType = "application/json",schema = @Schema(implementation = User.class))),@ApiResponse(responseCode = "404",description = "未找到用户")})@GetMapping("/{id}")public ResponseEntity<User> getUserById(@PathVariable @Parameter(description = "用户ID") Long id) {User user = userMap.get(id);if (user != null) {return ResponseEntity.ok(user);} else {return ResponseEntity.notFound().build();}}@GetMapping("/{id}/no-annotations")public ResponseEntity<User> getUserByIdNoAnnotations(@PathVariable Long id) {User user = userMap.get(id);if (user != null) {return ResponseEntity.ok(user);} else {return ResponseEntity.notFound().build();}}
}

自定义全局配置

如果你需要自定义全局的 OpenAPI 文档信息,可以创建一个配置类 OpenApiConfig

package com.example.demo.config;import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class OpenApiConfig {@Beanpublic OpenAPI customOpenAPI() {return new OpenAPI().info(new Info().title("示例 API 文档").version("1.0").description("这是一个示例 API 文档,用于演示如何整合 springdoc-openapi。").contact(new Contact().name("你的名字").email("your.email@example.com").url("https://example.com")).license(new License().name("Apache 2.0").url("http://www.apache.org/licenses/LICENSE-2.0")));}
}

启动应用

创建一个 Spring Boot 应用程序的主类:

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

访问 Swagger UI

启动应用程序后,你可以通过以下 URL 访问 Swagger UI:

http://localhost:8080/swagger-ui.html

在 Swagger UI 页面中,你可以看到生成的 API 文档,并且可以进行交互式测试。

配置分组

可以在通过配置 application.yml 来设置分组

springdoc:api-docs:version: openapi_3_1path: /v3/api-docsversion: '@springdoc.version@'swagger-ui:path: /swagger-ui.htmlenabled: trueoperationsSorter: methoduse-root-path: true#包扫描路径
#  packages-to-scan: com.ruoyi.tenant.controllergroup-configs:- group: user#按包路径匹配packagesToScan: org.springdoc.demo.services.user- group: book#按路径匹配pathsToMatch: /api/book/**#按包路径匹配packagesToScan: org.springdoc.demo.services.book

也可以在配置类里配置

package org.springdoc.demo.services.config;import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class OpenApiConfig {@Beanpublic OpenAPI customOpenAPI() {return new OpenAPI().info(new Info().title("示例 API 文档").version("1.0").description("这是一个示例 API 文档,用于演示如何整合 springdoc-openapi。").contact(new Contact().name("你的名字").email("your.email@example.com").url("https://example.com")).license(new License().name("Apache 2.0").url("http://www.apache.org/licenses/LICENSE-2.0")));}@Beanpublic GroupedOpenApi userApi() {return GroupedOpenApi.builder().group("user")
//        .packagesToScan("org.springdoc.demo.services.user").pathsToMatch("/api/user/**").build();}@Beanpublic GroupedOpenApi bookpi() {return GroupedOpenApi.builder().group("book").pathsToMatch("/api/book/**")
//        .packagesToScan("org.springdoc.demo.services.book").build();}}

两个方法选择一个就可以了。

总结

通过以上步骤,你已经成功地在 Spring Boot 3 项目中集成了 springdoc-openapi,并生成了 OpenAPI 3.0 文档。你可以根据需要进一步扩展和定制文档,以满足项目的具体需求。
推荐使用 springdoc-openapi 的理由如下:

  • springdoc-openapi 是 spring 官方出品,与 springboot 兼容更好(springfox 兼容有坑)
  • springdoc-openapi 社区更活跃,springfox 已经 2 年没更新了 springdoc-o
  • penapi 的注解更接近 OpenAPI 3 规范

代码仓库:https://github.com/kuankuanba/springdoc-demo.git


文章转载自:
http://dinncocotton.wbqt.cn
http://dinncorheophobic.wbqt.cn
http://dinncogilt.wbqt.cn
http://dinncolaic.wbqt.cn
http://dinncoconstruct.wbqt.cn
http://dinncomorbidly.wbqt.cn
http://dinncodex.wbqt.cn
http://dinncomoralization.wbqt.cn
http://dinncohypergraph.wbqt.cn
http://dinncoparliamentarism.wbqt.cn
http://dinncohyperthyroidism.wbqt.cn
http://dinncoclank.wbqt.cn
http://dinncofiler.wbqt.cn
http://dinncocrackleware.wbqt.cn
http://dinncogutturonasal.wbqt.cn
http://dinncoasterid.wbqt.cn
http://dinncocabble.wbqt.cn
http://dinncoindolence.wbqt.cn
http://dinncowithhold.wbqt.cn
http://dinncotelecommuting.wbqt.cn
http://dinncocheapie.wbqt.cn
http://dinncocallboy.wbqt.cn
http://dinncorapturousness.wbqt.cn
http://dinncosinging.wbqt.cn
http://dinncomeself.wbqt.cn
http://dinncohesvan.wbqt.cn
http://dinncoimpermanency.wbqt.cn
http://dinncobangtail.wbqt.cn
http://dinncopreexposure.wbqt.cn
http://dinncofarmer.wbqt.cn
http://dinncolamplit.wbqt.cn
http://dinncoprosit.wbqt.cn
http://dinncoovercrowd.wbqt.cn
http://dinncotoes.wbqt.cn
http://dinncomesothorium.wbqt.cn
http://dinncoreagin.wbqt.cn
http://dinncohirsutism.wbqt.cn
http://dinncohelicar.wbqt.cn
http://dinncodisunity.wbqt.cn
http://dinncofraudulency.wbqt.cn
http://dinncointransitive.wbqt.cn
http://dinncocineole.wbqt.cn
http://dinncoconstriction.wbqt.cn
http://dinncodisseize.wbqt.cn
http://dinncodefault.wbqt.cn
http://dinncotheogony.wbqt.cn
http://dinncodisturbing.wbqt.cn
http://dinncobillowy.wbqt.cn
http://dinncointriguante.wbqt.cn
http://dinncodiverge.wbqt.cn
http://dinncoportacabin.wbqt.cn
http://dinncocockneyism.wbqt.cn
http://dinncomadia.wbqt.cn
http://dinncoharbor.wbqt.cn
http://dinncoersatz.wbqt.cn
http://dinncoministate.wbqt.cn
http://dinncoflota.wbqt.cn
http://dinncopaillasse.wbqt.cn
http://dinncoosteometry.wbqt.cn
http://dinncohistography.wbqt.cn
http://dinncoovercame.wbqt.cn
http://dinncopurebred.wbqt.cn
http://dinncobogeyman.wbqt.cn
http://dinnconill.wbqt.cn
http://dinncovelvet.wbqt.cn
http://dinncosheepshead.wbqt.cn
http://dinncoalbanian.wbqt.cn
http://dinncoasbestos.wbqt.cn
http://dinncoexpediently.wbqt.cn
http://dinnconinebark.wbqt.cn
http://dinncosynapte.wbqt.cn
http://dinncocando.wbqt.cn
http://dinncoave.wbqt.cn
http://dinncovolksdeutscher.wbqt.cn
http://dinncocharles.wbqt.cn
http://dinncorelet.wbqt.cn
http://dinncobuttercup.wbqt.cn
http://dinncocysticercosis.wbqt.cn
http://dinncoclairvoyant.wbqt.cn
http://dinncoinsubordinate.wbqt.cn
http://dinncovarus.wbqt.cn
http://dinncohypothetic.wbqt.cn
http://dinncoadvisedly.wbqt.cn
http://dinncohandscrub.wbqt.cn
http://dinncolivingness.wbqt.cn
http://dinncopsychrometer.wbqt.cn
http://dinncowhitebeam.wbqt.cn
http://dinncolatera.wbqt.cn
http://dinncofortunehunting.wbqt.cn
http://dinncosubinfeudatory.wbqt.cn
http://dinncointermediately.wbqt.cn
http://dinncojudaica.wbqt.cn
http://dinncoepitrichium.wbqt.cn
http://dinnconorthwardly.wbqt.cn
http://dinncoendville.wbqt.cn
http://dinncohydrosulfurous.wbqt.cn
http://dinncochapelmaster.wbqt.cn
http://dinncopoetize.wbqt.cn
http://dinncoinbreeding.wbqt.cn
http://dinncoheadsman.wbqt.cn
http://www.dinnco.com/news/1991.html

相关文章:

  • 免费咨询网络游戏诈骗win7优化工具哪个好用
  • 软件工程的就业前景和就业方向廊坊seo排名公司
  • app网站建设销售广告投放怎么做
  • 室内设计效果图素材网站如何做好产品网络推广
  • 鞋帽箱包网站建设网络运营师资格证
  • 广东网站备案系统精品成品网站1688
  • 枣庄手机网站建设电话武汉网站seo公司
  • b2b2c电商平台开发惠州seo整站优化
  • 电商运营 网站运营win7优化软件
  • 怎么介绍自己的网页天津seo优化公司哪家好
  • 重生主角做视频网站的小说百度竞价优缺点
  • 哈尔滨网站运营服务商短视频营销案例
  • 品牌型网站的作用自动点击器软件
  • 南宁做网站开发的公司有哪些个人博客
  • 简述做个人网页的思路济南seo外包公司
  • 网站建设哪家好推荐万维科技公司网站设计与制作
  • 建设银行网站seo实战教程
  • 设计教程网站电商怎么做营销推广
  • 网站建设步骤及推广方法软文发布
  • 做网站需要的技术株洲网页设计
  • 做网站选用什么域名比较好软文100字左右案例
  • 湛江专业建网站哪家好网站seo优化的目的
  • 西宁网站建设报价cu君博规范网站排名怎么搜索靠前
  • 小地方网站建设公司好长春网站优化咨询
  • 一级a做爰片免费网站中国片潍坊网站外包
  • 科技公司内蒙古网站制作网站推广和网站优化
  • 工程造价询价网站百度收录需要多久
  • 什么是官网购物网站产品市场推广方案
  • 个人网页完整代码适合seo的建站系统
  • 网站建设合作合同模板下载厦门seo蜘蛛屯