Spring Boot集成SpringDoc生成Api文档

以下以 Maven 为例介绍 Spring Boot集成SpringDoc生成Api文档。 添加依赖 1 2 3 4 <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> </dependency> 配置 annotationProcessor,实现通过 javadoc 生成文档。 每个 maven 模块都需要配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <properties> <therapi-runtime-javadoc.version>0.15.0</therapi-runtime-javadoc.version> <maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version> </properties> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <annotationProcessorPaths> <!-- https://springdoc.org/#javadoc-support --> <path> <groupId>com.github.therapi</groupId> <artifactId>therapi-runtime-javadoc-scribe</artifactId> <version>${therapi-runtime-javadoc.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> 配置 spring boot 插件,生成 build.properties 1 2 3 4 5 6 7 8 9 10 11 12 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> <goal>build-info</goal> </goals> </execution> </executions> </plugin> 自动装配 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 @Configuration(proxyBeanMethods = false) @ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) public class SpringdocConfig { @Value("${server.port}") private String port; @Value("${openapi.prod-url:https://localhost}") private String prodUrl; @Bean public OpenAPI openAPI() { Server devServer = new Server(); devServer.setUrl("http://localhost:" + port); devServer.setDescription("Server URL in Development environment"); Server prodServer = new Server(); prodServer.setUrl(prodUrl); prodServer.setDescription("Server URL in Production environment"); Contact contact = new Contact(); contact.setEmail("[email protected]"); contact.setName("ChenSoul"); contact.setUrl("https://blog.chensoul.cc"); License mitLicense = new License().name("Apache License").url("https://www.apache.org/licenses/LICENSE-2.0.txt"); Info info = new Info() .title("Spring Boot3 Monolith API") .version("1.0") .contact(contact) .description("This API exposes endpoints to manage charging sessions.").termsOfService("https://blog.chensoul.cc/terms") .license(mitLicense); return new OpenAPI().info(info).servers(List.of(devServer, prodServer)); } }

2024-07-10 · 1 min · 298 words · chensoul

使用 Docker 容器化并运行 Spring Boot 应用程序

本文翻译自 Docker 官方网站的《Java language-specific guide》文章,并做了一些改动。 Java 入门指南教您如何使用 Docker 创建容器化的 Spring Boot 应用程序。在本模块中,您将学习如何: 使用 Maven 容器化并运行 Spring Boot 应用程序 设置本地开发环境以将数据库连接到容器,配置调试器,并使用 Compose Watch 进行实时重新加载 在容器内运行单元测试 使用 GitHub Actions 为应用程序配置 CI/CD 管道 将容器化应用程序本地部署到 Kubernetes 以测试和调试您的部署 完成 Java 入门模块后,您应该能够根据本指南中提供的示例和说明来容器化您自己的 Java 应用程序。 ...

2024-07-09 · 13 min · 6263 words · chensoul

Spring Boot项目创建Docker镜像并运行应用

手动创建 Dockerfile 添加 Dockerfile 在您的 Spring Boot 项目根目录下创建一个名为 Dockerfile 的文件,并添加以下内容: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # 使用 OpenJDK 21 作为基础镜像 FROM openjdk:21 # 设置工作目录 WORKDIR /app # 将 JAR 文件复制到容器中 COPY target/*.jar app.jar # 暴露 8080 端口 EXPOSE 8080 # 设置容器启动时执行的命令 ENTRYPOINT ["java", "-jar", "app.jar"] 这个 Dockerfile 将使用 OpenJDK 21 作为基础镜像,将编译后的 JAR 文件复制到容器中,并在容器启动时执行 java -jar app.jar 命令。 ...

2024-06-06 · 7 min · 3284 words · chensoul

[译]使用 Spring Boot 构建 RESTful API:集成 DDD 和六边形架构

原文链接:Building a RESTful API with Spring Boot: Integrating DDD and Hexagonal Architecture 介绍 在快节奏的软件开发世界中,API 扮演着至关重要的角色,可以有效地促进不同系统之间的交互和数据交换。API 创建最突出的技术之一是 Spring Boot,它是一种强大的工具,可以简化 Java 应用程序的开发,使开发人员可以专注于业务逻辑而不是环境设置。 ...

2024-05-30 · 24 min · 11759 words · chensoul

2024-01-17|MySQL 主从复制、ShardingJDBC实现读写分离、集成Springdoc+Javadoc

今天做了什么: 观看《2022年黑马程序员新版java课程》中 MySQL 主从复制和读写分离相关视频,使用 Docker 搭建 MySQL 主从复制环境。 foodie-cloud 项目实现读写分离并集成 Springdoc Docker 搭建 MySQL 主从复制环境 参考文章 :基于 Docker 的 MySQL 主从复制搭建及原理(真正弄懂) ...

2024-01-17 · 3 min · 1284 words · chensoul

[译]Spring Boot3和Spring6中的新特性

Spring Boot 3.0 于 2022 年 11 月正式发布,包含一些新功能和改进。这是继大约 4.5 年前发布 Spring Boot 2.0 后 Spring Boot 的第一个主要版本。它也是第一个支持 Spring Framework 6.0 的 Spring Boot GA 版本。作为开发人员,我们需要了解这些更新,才能顺利使用 Spring Boot。毫无疑问,新版本中最大的转变之一是放弃了对旧版本 Java 的支持。 ...

2023-10-13 · 6 min · 2581 words · chensoul

[译]REST API 的自定义错误消息处理

1. 概述 在本教程中,我们将讨论如何为 Spring REST API 实现全局错误处理程序。 我们将使用每个异常的语义为客户端构建有意义的错误消息,其明确的目标是为客户端提供所有信息以轻松诊断问题。 2. 自定义错误消息 让我们首先实现一个用于通过线路发送错误的简单结构 — ApiError: ...

2023-08-25 · 4 min · 1964 words · chensoul

[译]使用Spring进行REST的错误处理

1. 概述 本教程将说明如何使用 Spring 为 REST API 实现异常处理。我们还将获得一些历史概述,并了解不同版本引入了哪些新选项。 在 Spring 3.2 之前,Spring MVC 应用程序中处理异常的两种主要方法是 HandlerExceptionResolver 或 @ExceptionHandler 注解。两者都有一些明显的缺点。 从 3.2 开始,我们使用了 @ControllerAdvice 注释来解决前两个解决方案的局限性,并促进整个应用程序的统一异常处理。 ...

2023-08-25 · 8 min · 3554 words · chensoul

[译]如何在Spring中执行@Async

1. 概述 在本教程中,我们将探讨 Spring 中的异步执行支持和 @Async 注解。 简单地说,用 @Async 注解 bean 的方法将使其在单独的线程中执行。换句话说,调用者不会等待被调用方法的完成。 Spring 的一个有趣的方面是,框架中的事件支持还 支持异步处理(如果需要)。 2.启用异步支持 让我们首先通过 Java 注解启用异步处理。 ...

2023-08-25 · 3 min · 1444 words · chensoul

[译]为 Spring Boot 应用程序创建优化的 Docker 映像

容器已成为打包具有所有软件和操作系统依赖项的应用程序,然后将其传送到不同环境的首选方式。 本文着眼于容器化 Spring Boot 应用程序的不同方法: 使用 Docker 文件构建 Docker 镜像, 使用 Cloud-Native Buildpack 从源代码构建 OCI 映像, 通过使用分层工具将 JAR 的各个部分拆分为不同的层,在运行时优化映像。 示例代码 本文附有 GitHub 上的工作代码示例。 ...

2023-08-16 · 8 min · 3818 words · chensoul