我使用 webflux 应用程序创建了一个 Spring boot 3,最重要的是我首先使用 swagger 添加了 opanapi 和合约,我还在 openapi 中激活了 HATEOAS,因为模型是在使用 maven 安装时生成的,我无法使用 hatoas 插件。
我的问题是,它不仅在输出对象上添加链接对象,还在我们完全不需要的输入上添加链接对象,此输入在我的应用程序中用于不理解“链接”的第三个 api 调用。第一个解决方案只是创建一个新的对象和映射器,但将来可能会导致 api 出现问题,所以我想从输入对象中删除链接对象,我该怎么做?
我的插件配置:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.4.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/swagger.yaml</inputSpec>
<generatorName>spring</generatorName>
<library>spring-boot</library>
<apiPackage>com.telr.tokenization.api</apiPackage>
<modelPackage>com.telr.tokenization.core.entities</modelPackage>
<supportingFilesToGenerate>false</supportingFilesToGenerate>
<configOptions>
<disallowAdditionalPropertiesIfNotPresent>true
</disallowAdditionalPropertiesIfNotPresent>//not working
<hateoas>true</hateoas>
<reactive>true</reactive>
<interfaceOnly>true</interfaceOnly>
<skipDefaultInterface>true</skipDefaultInterface>
<useJakartaEe>true</useJakartaEe>
//I tested this not working
<additionalModelTypeAnnotations>
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
</additionalModelTypeAnnotations>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
yaml 示例:
paths:
/v1/tokens:
post:
tags:
- Network token
summary: Create network token
description: Create network token
operationId: createNetworkToken
requestBody:
description: Create a new network token in app
content:
application/json:
schema:
$ref: '#/components/schemas/Card'
required: true
responses:
'201':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/NetworkTokenInformationResponse'
'400':
description: Bad Request
'401':
description: Unauthorized
'500':
description: Internal server error
/v1/tokens/{gatewayToken}:
get:
tags:
- "Network token"
summary: "Retrieve token"
description: "Returns a network token"
operationId: "getToken"
parameters:
- name: gatewayToken
in: path
description: Gateway token generated in the network tokenization process
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/NetworkTokenInformationResponse'
'400':
description: Bad Request
'401':
description: Unauthorized
'404':
description: Token not found
'500':
description: Internal server error
components:
schemas:
NetworkTokenInformationResponse:
type: object
required:
- gatewayToken
properties:
gatewayToken:
type: string
token:
$ref: '#/components/schemas/NetworkToken'
NetworkToken:
type: object
properties:
enrollmentId:
type: string
tokenReferenceId:
type: string
dPan:
type: string
expMonth:
type: string
expYear:
type: string
Card:
type: object
required:
- number
- expirationMonth
- expirationYear
properties:
number:
type: string
expirationMonth:
type: string
expirationYear:
type: string
securityCode:
type: string
知道如何从所有输入对象中抑制此链接对象吗?
然后
pom.xml
配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.21</version>
</dependency>
最后,我没有直接修改对象本身,但为了序列化,我在 pom.xml 中添加了以下配置,以使用注释修改生成的类
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.4.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/swagger.yaml</inputSpec>
<generatorName>spring</generatorName>
<library>spring-boot</library>
<apiPackage>com.telr.tokenization.api</apiPackage>
<modelPackage>com.telr.tokenization.core.entities</modelPackage>
<supportingFilesToGenerate>false</supportingFilesToGenerate>
<configOptions>
<additionalModelTypeAnnotations>@lombok.Builder @lombok.AllArgsConstructor @lombok.EqualsAndHashCode(callSuper = true)
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY)
</additionalModelTypeAnnotations>
<hateoas>true</hateoas>
<reactive>true</reactive>
<interfaceOnly>true</interfaceOnly>
<skipDefaultInterface>true</skipDefaultInterface>
<useJakartaEe>true</useJakartaEe>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
事实上,当我在 josn 中发送此对象时,它将清除所有 null 和空对象或我需要的列表。