简体   繁体   English

如何使用 swagger-codegen 为 Java 生成具有不同控制器名称的 API?

[英]How to generate API with swagger-codegen with a different controller name for Java?

swagger-codegen generates APIControllers based on pathname. swagger-codegen 根据路径名生成 APIController。

Let's say you have these paths on your swagger.yaml:假设您的 swagger.yaml 上有这些路径:

/pet/findByStatus: /user/{userId} /store/inventory /pet/findByStatus: /user/{userId} /store/inventory

Then codegen is going to generate PetAPIController, UserAPIController, StoreApiController.然后 codegen 将生成 PetAPIController、UserAPIController、StoreApiController。

But my API is something like:但我的 API 是这样的:

/private/pet/findByStatus: /private/user/{userId} /public/store/inventory /private/pet/findByStatus: /private/user/{userId} /public/store/inventory

so I end up with two main controllers: PrivateAPI and PublicAPI所以我最终得到了两个主要的控制器:PrivateAPI 和 PublicAPI

Is there any way to avoid this?有什么办法可以避免这种情况吗? Using tags, or just with the second path word.使用标签,或仅使用第二个路径词。

Thanks谢谢

Swagger Codegen's spring generator has the useTags option (true/false) that tells the codegen to use tags to name the interface and controller classes. Swagger Codegen 的spring生成器具有useTags选项(true/false),它告诉useTags使用标签来命名接口和控制器类。 When using the Swagger Codegen Maven plugin, you can specify this option in the <configOptions> section:使用 Swagger Codegen Maven 插件时,您可以在<configOptions>部分指定此选项:

<configuration>
    <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
    <language>spring</language>
    <configOptions>
       <useTags>true</useTags>
    </configOptions>
</configuration>

When useTags = true , operations with a specific tag, say admin , will be placed into AdminApi.java and AdminApiController.java .useTags = true ,带有特定标签的操作,比如admin ,将被放置到AdminApi.javaAdminApiController.java 中

Make sure to tag all operations in your API definition appropriately:确保适当地标记 API 定义中的所有操作:

paths:
  /foo:
    get:
      tags:    # <-----
        - admin
      ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM