Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2017-01-13 18:50:03

I am new to swagger codegen and I use version 2.1.6. I am trying to generate rest service via yaml and swagger-codegen-maven. Below is part of 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>  

ServiceImpl generated by swagger is:

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();
  }

}

Here, instead of return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();, I would like to map request processing to my own request handler and I don't want to hardcode it to rest api.

The idea is, rest-api is to be reusable (jar file). The system that includes rest-api as dependency jar will take care of building war. Packaging structure is a sort of stone carved requirement given to me. :)

Does anyone know how do I override this ServiceImpl code WITHOUT doing some crazy stuff like overriding DefaultCodeGen etc ? Is it possible to use templates to achieve this?

Any input will be gratefully appreciated.

Questioner
bkrish
Viewed
0
moondaisy 2017-01-20 22:05:07

The template that generates that part of the code is this one. You should be able to achieve the behaviour you need by changing the template.