我正在使用 SpringBoot 3.4.1、Java 21,并且正在尝试生成 Spring 服务器类(Api plus Model)。
我在生成器项目模块上有这个 maven 配置:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.7.0</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.10.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api-docs.yaml</inputSpec>
<output>${project.build.directory}/generated-sources</output>
<generatorName>spring</generatorName>
<apiPackage>com.api.rest.web.resources</apiPackage>
<modelPackage>com.api.rest.model</modelPackage>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
我还编写了这个简单的.yaml
文件来测试生成器:
openapi: "3.0.2"
info:
title: Example Rest Archetype
version: 0.0.1
components:
schemas:
User:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
minLength: 3
maxLength: 20
surname:
type: string
minLength: 3
maxLength: 20
username:
type: string
minLength: 3
maxLength: 30
email:
type: string
minLength: 3
maxLength: 30
address:
type: array
items:
$ref: "#/components/schemas/Address"
required:
- name
- surname
- username
Address:
type: object
properties:
id:
type: integer
format: int64
description:
type: string
minLength: 3
maxLength: 20
user:
type: object
$ref: "#/components/schemas/User"
Error:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
required:
- code
- message
就是这样。我知道paths
缺少该属性,但是当我编译模块时,我收到另外两个错误,我不明白为什么。结果如下:
Errors:
-attribute paths is missing
-attribute components.schemas.Error.properties is not of type `object`
-attribute components.schemas.User.properties is not of type `object`
怎么了?
我修正了我的.yaml
文件,因为我使用了 IntelliJ 编辑器,缩进错误可能是由于在这里将其格式化为代码而添加了空格。无论如何,我错过了jackson
对我的依赖pom.xml
,这肯定导致了错误。
多谢。
您的 yaml 中存在一些缩进错误。我添加了缺失部分
paths: {}
以启用构建。修正文件
为了生成 Jakarta 注释,我添加了
<useJakartaEe>true</useJakartaEe>
。完整的 POM