Jackson 反序列化 json:JSON 字符串中的字段 `form_object_guid` 未在 `DigitalFormGetResponse` 属性中定义

问题描述 投票:0回答:1

我查询 GET 并获得响应 JSON 字符串,但脱盐抛出 The field

form_object_guid
in the JSON string is not Defined in the
DigitalFormGetResponse
properties,这意味着字段
form_object_guid
在类
DigitalFormGetResponse
中不存在。类
DigitalFormGetResponse
是从 Maven 工件 openapi-generator-maven-plugin 版本 6.6.0 自动生成的,不包含 jersey2 作为库。在我向插件
<additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>
添加注释后,生成的代码显示类
DigitalFormGetResponse
具有 IgnoreProperties 注释,我认为它将忽略检索到的 JSON 字符串中的未知属性。

@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)
public class DigitalFormGetResponse {

但是,我从自动生成的函数中得到了异常:

我之前的版本在插件中使用了 jersey2,并且可以很好地忽略未知属性。但是,我必须将 jersey2 从插件中移出(因为它不支持 Java16+ 的 PATH 请求),然后这个忽略功能就成为一个问题。

POM文件

    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>6.6.0</version>
        <executions>
            <execution>
                <id>spring-boot-api</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>${project.basedir}/digitalformsords.yaml</inputSpec>
                    <generatorName>java</generatorName>
                    <configOptions>
                        <dateLibrary>joda</dateLibrary>
                        <validateSpec>false</validateSpec>
                        <useSpringBoot3>true</useSpringBoot3>
                        <useJakartaEe>true</useJakartaEe>
                        <additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>
                    </configOptions>
                    <library>jersey2</library>
                    <apiPackage>${default-package}.api</apiPackage>
                    <modelPackage>${default-package}.api.model</modelPackage>
                    <invokerPackage>${default-package}.api.handler</invokerPackage>
                    <generateModelTests>false</generateModelTests>
                    <generateApiTests>false</generateApiTests>
                </configuration>
            </execution>
        </executions>
    </plugin>
spring-boot jackson jersey openapi-generator-maven-plugin
1个回答
0
投票

我在 POM 文件中添加了 maven-replacer-plugin 以删除对 validateJsonObject(jsonObj); 的调用;所以不会发生异常。我认为这不是最好的方法,但我必须保留现有逻辑,以免强制进行 JSON 验证。

    <plugin>
        <groupId>com.google.code.maven-replacer-plugin</groupId>
        <artifactId>replacer</artifactId>
        <version>1.5.3</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>replace</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <includes>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/ErrorMessage.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/HealthOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsDisclosureSentOrdsRequest.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsDisclosureSentOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsDocumentOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsProhibitionStatusOrdsResponse.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsProhibitionStatusOrdsResponseDisclosureInner.java</include>
                <include>${project.build.directory}/generated-sources/openapi/src/main/java/open/jag/ordsvipsclient/api/model/VipsProhibitionStatusOrdsResponseReviewsInner.java</include>
            </includes>
            <replacements>
                <replacement>
                    <token>validateJsonObject\(jsonObj\);</token>
                    <value></value>
                </replacement>
            </replacements>
        </configuration>
    </plugin>
</plugins>
© www.soinside.com 2019 - 2024. All rights reserved.