首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用LinkedHashMap而不是HashMap作为Swagger字典类型(Java codegen)?

使用LinkedHashMap而不是HashMap作为Swagger字典类型(Java codegen)?
EN

Stack Overflow用户
提问于 2020-02-26 20:32:07
回答 1查看 541关注 0票数 2

我使用的是openapi 3.0.2和codegen插件:

代码语言:javascript
运行
复制
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.14</version>

我使用了下面描述的swagger字典/hashmap类型:

https://swagger.io/docs/specification/data-models/dictionaries/

代码语言:javascript
运行
复制
openapi: "3.0.2"
...
Labels:
    type: object
    additionalProperties:
        type: string
        minLength: 1
    description:  A description.

当我为此生成Java代码时,它被建模为一个扩展HashMap的类

代码语言:javascript
运行
复制
@ApiModel(description = "A description.")
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2020-02-26T12:17:36.248Z[Europe/London]")
public class Labels extends HashMap<String, String>  {
...
}

有没有办法指示swagger使用LinkedHashMap而不是HashMap?(不必从codegen中排除这个类并手动修改它)。

我希望在将此字典返回给客户端时控制此字典中条目的顺序。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-12 00:27:44

我使用了一个maven插件来修改生成的文件:

代码语言:javascript
运行
复制
<plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <executions>
                <execution>
                        <id>modify-swagger</id>
                        <phase>generate-sources</phase>
                        <goals>
                                <goal>execute</goal>
                        </goals>
                        <configuration>
                                <properties>
                                        <property>
                                                <name>targetDir</name>
                                                <value>${project.build.directory}</value>
                                        </property>
                                </properties>
                                <scripts>
                                        <script><![CDATA[
                                                def labelsFile = new File(targetDir
                                                                + "/swagger-codegen/src/main/java/com/acme/models/Labels.java")
                                                def labelsFileContents = labelsFile.text
                                                if (labelsFileContents == null) {
                                                        throw Exception();
                                                }

                                                def newLabelsFileContents = labelsFileContents.replaceAll('HashMap', 'LinkedHashMap')
                                                labelsFile.write(newLabelsFileContents)
                                        ]]></script>
                                </scripts>
                        </configuration>
                </execution>
        </executions>
</plugin>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60414026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档