---
swagger: "2.0"
definitions:
Result:
properties:
data:
type: string
description: Some data result
Payload:
properties:
id:
required: true
type: integer
data:
required: true
type: string
在我生成Swagger POJO后,我在类这样的类中的方法中创建。
public Result sendMessage(int id, String data) throws IOException {
return some_clazz.go(
Result.class,
new Payload().id(id).data(data)
);
}
问题是我有许多不同的定义,我几乎只是复制模式,因为他们每个都使用常见的some_clazz.go
方法(注意:some_clazz使用genetics
因此第一个arg)。例如:
public AnotherResult sendMessage(int id) throws IOException {
return some_clazz.go(
AnotherResult.class,
new DifferentPayload().id(id)
);
}
有没有办法让swagger(或其他东西)从我的定义中生成可以传递给some_clazz.go
的类和方法?
我不确定我会遵循some_clazz.go所做的事情,或者你想要发送给它的是什么。但是要尝试回答你的问题 - 你可以扩展swagger-codegen。
为了做到这一点,你必须通过扩展DefaultCodegen来扩展当前的swagger-codegen,或者更有可能扩展你当前正在使用的Codegen(SpringCodegen,AbstractJavaCodegen等,无论什么对你有用)。在新的codegen类中,您需要添加一个新模板
public void processOpts() {
super.processOpts();
supportingFiles.add(new SupportingFile("someclazzgo.mustache", invokerFolder, "SomeClazzGoImpl.java"));
所以现在生成器将采用someclazzgo.mustache并根据您创建的模板创建SomeClazzGoImpl.java。由您来实现该模板。如果您需要,可以为每个操作或类实现一个方法。也许如果你提供更多关于你要做什么的详细信息,我可以提供更具体的例子(some_clazz.go是为每次调用调用还是只调用特定的API?它是否返回值?)
有关如何在https://github.com/swagger-api/swagger-codegen#making-your-own-codegen-modules中扩展codegen的更多细节