Mich44
Mich44

Reputation: 95

How to generate client api from different module

I'm building 2 microservices. One named cart, and second one named product. Both of them have API generated via swagger-codegen-maven-plugin, however now I want that cart microservice will have generated client api from yaml file deffinition which resides in product module (the one from which product have generated server client). How can I achive that? Is there a way to access yaml file from different module? What kind of dependency I have to have?

            <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>generate-cart-server</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/static/api.yaml</inputSpec>
                        <language>spring</language>
                        <configOptions>
                            <dateLibrary>joda</dateLibrary>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Project structure

Upvotes: 1

Views: 2113

Answers (1)

You can use relative paths as long as you are inside the same project. You have to add multiple execution tags to generate both, client for product and server for cart.

<executions>
    <execution>
        <id>generate-cart-server</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <inputSpec>${project.basedir}/src/main/resources/static/api.yaml</inputSpec>
            <language>spring</language>
            <configOptions>
                <dateLibrary>joda</dateLibrary>
            </configOptions>
        </configuration>
    </execution>
    <execution>
        <id>generate-product-client</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <inputSpec>${project.basedir}/../product/src/main/resources/static/api.yaml</inputSpec>
            <language>java</language>
            <library>resttemplate</library>
            <configOptions>
                <dateLibrary>joda</dateLibrary>
            </configOptions>
        </configuration>
    </execution>
</executions>

Use <language>java</language> to generate Java client code and choose a library you'd like to use with <library>...</library>. RestTemplate is a part of Spring, but there are many others.

Quoting from Baeldung:

 Swagger Codegen supports the following Java libraries (pairs of HTTP 
clients and JSON processing libraries):

    jersey1 – Jersey1 + Jackson
    jersey2 – Jersey2 + Jackson
    feign – OpenFeign + Jackson
    okhttp-gson – OkHttp + Gson
    retrofit (Obsolete) – Retrofit1/OkHttp + Gson
    retrofit2 – Retrofit2/OkHttp + Gson
    resttemplate – Spring RestTemplate + Jackson
    rest-easy – Resteasy + Jackson

Of course, you need to add the selected library to your dependencies as well.

Upvotes: 2

Related Questions