如何覆盖默认的 swagger-code-gen 服务实现?

How to override default swgger-code-gen Service implementation?

我是 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 的系统将负责构建 war。包装结构是给我的一种石雕要求。 :)

有谁知道我如何覆盖这个 ServiceImpl 代码而不做一些疯狂的事情,比如覆盖 DefaultCodeGen 等?是否可以使用模板来实现?

任何输入将不胜感激。

生成那部分代码的模板是this one。 您应该能够通过更改模板来实现所需的行为。

当您想使用自定义模板时,您可以使用以下方法:

在 maven 中添加另一个插件来下载包含自定义模板的 maven 依赖项,例如,您可以将它们上传到本地工件。 在这种情况下,工件称为 swagger-templates 及其版本 0.0.1.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-remote-resources-plugin</artifactId>
  <configuration>
    <resourceBundles>
      <resourceBundle>our.mvn.group:swagger-templates:0.0.1</resourceBundle>
    </resourceBundles>
  </configuration>
  <executions>
    <execution>
      <phase>initialize</phase>
      <goals>
        <goal>process</goal>
      </goals>
    </execution>
  </executions>
</plugin>

然后在 swagger-codegen-maven-plugin 配置部分确保引用正确的目录,添加:

<templateDirectory>${project.build.directory}/maven-shared-archive-resources/subdir</templateDirectory>

其中 subdir 是包含自定义模板的 Maven 工件的 src/main/resources 目录中的子目录。你可以把你的小胡子文件放在那里。