java - 如何在从 swagger 生成 java 代码时将 "double"字段视为 "BigDecimal"?

标签 java rest swagger swagger-codegen

我正在 Swagger 生成 pojo。 swagger 由第三方提供,无法更改。我希望将“双”字段生成为“BigDecimal”。如何自定义我的代码生成器以实现此目的?

        <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>generateSquiree</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/schema/sample.swagger.json</inputSpec>
                        <configOptions>
                            <modelPackage>${basepackage}.model</modelPackage>
                            <apiPackage>${basepackage}.api</apiPackage>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <language>spring</language>
                <configOptions>
                    <serializableModel>true</serializableModel>
                    <java8>false</java8>
                    <javaVersion>${java.version}</javaVersion>
                    <jdk8>true</jdk8>
                    <dateLibrary>joda</dateLibrary>
                    <useTags>true</useTags>
                    <sourceFolder>src/main/java</sourceFolder>
                    <interfaceOnly>true</interfaceOnly>
                </configOptions>
            </configuration>
        </plugin>

下面是 swagger 的一个片段,需要生成为 “小数”

    "Quantity": {
      "description": "Represent a quantity",
      "required": [
        "Amount"
      ],
      "type": "object",
      "properties": {
        "Amount": {
          "format": "double",
          "description": "Amount",
          "type": "number"
        }
      }
    },

最佳答案

在以下位置找到了我的问题的答案

https://github.com/swagger-api/swagger-codegen/issues/5587#issuecomment-368805748

如果有人有同样的问题,请在下面发布解决方案

        <configuration>
            ....
            <typeMappings>
                <typeMapping>Double=java.math.BigDecimal</typeMapping>
            </typeMappings>
        </configuration>

关于java - 如何在从 swagger 生成 java 代码时将 "double"字段视为 "BigDecimal"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654672/

相关文章:

java - Gson json无法获取结果

java.lang.ClassCastException : org. apache.pdfbox.cos.COSArray

c# - 应该首选哪个依赖注入(inject)生命周期?

python - 使用 django-piston 时出现 400 Bad Request 错误

asp.net-core - .NET Core 3.1 - Swagger 未加载

spring - NoSuchMethodError : com. sun.jersey.core.reflection.ReflectionHelper.getOsgiRegistryInstance()Lcom/sun/jersey/core/osgi/OsgiRegistry;

java - 为什么 write() 和 read() 似乎写入和读取超过一个字节?

Java 最大文件大小

php - 使用 Swagger/OpenAPI 规范指定要作为 GET 参数发送的数组

javascript - swagger-无法识别的响应类型;将内容显示为文本