我是 swagger codegen 的新手,我使用的是 2.1.6 版。我正在尝试通过 yaml 和 swagger-codegen-maven 生成休息服务。下面是 pom.xml 的一部分:
.....
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${io.swagger.swagger-codegen-maven-plugin.version}</version>
<executions>
<execution>
<id>createprod</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/swagger/rest-api-create-product.yaml</inputSpec>
<language>io.swagger.codegen.languages.JavaResteasyServerCodegen</language>
<!-- <templateDirectory>How_do_I_use_it</templateDirectory> -->
<output>${project.build.directory}/generated-sources</output>
<apiPackage>${swagger.resourcePackage}.handler</apiPackage>
<modelPackage>${swagger.resourcePackage}.model</modelPackage>
<invokerPackage>${swagger.resourcePackage}.handler</invokerPackage>
<configOptions>
<sourceFolder>src/main/java</sourceFolder>
<interfaceOnly>true</interfaceOnly>
<configPackage>com.domain.service.configuration</configPackage>
<serializableModel>true</serializableModel>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
swagger生成的ServiceImpl为:
import com.domain.servie.handler.*;
import com.domain.servie.model.*;
import com.domain.servie.model.InitSuccessResponse;
import com.domain.servie.model.FailureResponse;
import com.domain.servie.model.InitRequest;
import java.util.List;
import com.domain.servie.handler.NotFoundException;
import java.io.InputStream;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-01-13T12:19:29.084-06:00")
public class InitWfApiServiceImpl extends InitWfApiService {
@Override
public Response createProductPost(String apiKey,InitRequest body,SecurityContext securityContext)
throws NotFoundException {
// do some magic!
return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
}
}
在这里,return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
我想将请求处理映射到我自己的请求处理程序,而不是,我不想将它硬编码到 rest api。
这个想法是,rest-api 是可重用的(jar 文件)。包含 rest-api 作为依赖 jar 的系统将负责构建战争。包装结构是一种石雕给我的要求。:)
有谁知道如何覆盖这个 ServiceImpl 代码而不做一些疯狂的事情,比如覆盖 DefaultCodeGen 等?是否可以使用模板来实现这一点?
任何输入将不胜感激。
生成那部分代码的模板就是这个。你应该能够通过更改模板来实现你需要的行为。
设置 mustache 模板后,我可以使用模板创建我的服务实现。但我遇到了奇怪的情况。因为我在 yaml 中有多个 API,所以我无法为不同的 API 设置多个服务实现。所以我采取的解决方法是创建多个 yaml 并运行 swagger-codegen。知道如何在不创建多个 yaml 的情况下容纳多个服务实现吗?
@bkrish 我很高兴它奏效了,但我不知道如何解决你的新场景,但问一个新问题,因为其他人可能知道。
明白了。感谢您的帮助!
没有解释如何使用 templateDirectory 覆盖模板?