Saturday, April 8, 2023

Rest client using swagger codegen

 https://stackoverflow.com/questions/57545884/how-to-develop-a-simple-rest-client-using-swagger-codegen

Yes. You can use swagger-codegen-maven-plugin to generate a REST client. But before that , you need to describe the REST API in YAML or JSON in OpenAPI Specification mainly because swagger-codegen-maven-plugin only can generate a REST client from a file written in this specification.

Other answers assume that you need to write the specification manually while my solution take a step further to automatically generating the specification from the REST controller source codes.

The latest OpenAPI version is 3.0 .But based on the package of your imported swagger annotation , you are using version 2.0 (or before). So my solution assume you are using OpenAPI 2.0.

Generating Open API Specification

First, you can use swagger-maven-plugin to generate an OpenAPI spec from the RestController source codes. It basically analyses the Swagger annotations annotated in the @RestController classes that specified in <locations> and dump the OpenAPI spec to /src/main/resources/swagger.json :

<plugin>
    <groupId>com.github.kongchen</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.1.5</version>
    <configuration>
        <apiSources>
            <apiSource>
                <springmvc>true</springmvc>
                <locations>
                    <location>com.dgs.spring.springbootswagger.controller.EmployeeController</location>
                    <location>com.dgs.spring.springbootswagger.controller.FooController</location>
                </locations>
                <schemes>
                    <scheme>http</scheme>
                </schemes>
                <host>127.0.0.1:8080</host>
                <basePath>/</basePath>
                <info>
                    <title>My API</title>
                    <version>1.1.1</version>
                </info>
                <swaggerDirectory>${basedir}/src/main/resources/</swaggerDirectory>
            </apiSource>
        </apiSources>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Execute the followings maven command to start generation:

mvn clean compile

Generating Rest Client

After swagger.json is generated, you can copy and paste it to your client project (e.g. /src/main/resources/swagger.json) . We can then use swagger-codegen-maven-plugin to generate a HTTP client .

By default , it will generate the whole maven project which includes test cases and other documentation stuff. But what I want is just the HttpClient 's source codes without other things. After several trial and error , I settle down to the following configuration :

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.4.7</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${basedir}/src/main/resources/swagger.json</inputSpec>
                <language>java</language>
                <library>resttemplate</library>
                <output>${project.basedir}/target/generated-sources/</output>

                <apiPackage>com.example.demo.restclient.api</apiPackage>
                <modelPackage>com.example.demo.restclient.model</modelPackage>
                <invokerPackage>com.example.demo.restclient</invokerPackage>

                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
                <generateApiDocumentation>false</generateApiDocumentation>
                <generateModelDocumentation>false</generateModelDocumentation>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                    <sourceFolder>restclient</sourceFolder>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

The generated HTTP client is based on RestTemplate and will be generated to the folder target/generated-sources/restclient. You may have to configure your IDE to import the generated client in order to use it . (In case of Eclipse, you can configure in Project Properties ➡️ Java Build Path ➡️ Add the folder of the generated rest client)

To start generating the client , just execute the maven command :

mvn clean compile

To use the generated HTTP client :

ApiClient apiClient = new ApiClient();

//Override the default API base path configured in Maven
apiClient.setBasePath("http://api.example.com/api");

EmployeeManagementSystemApi api = new EmployeeManagementSystemApi(apiClient);
api.getEmployeeById(1l);

Note :

  • If you come across javax/xml/bind/annotation/XmlRootElement exception during generation when using java8+ , you may need to refer to this.

Saturday, December 31, 2022

Rename a project in android-studio - veryuseful

 https://stackoverflow.com/questions/57102684/how-to-copy-and-rename-a-project-in-android-studio-3-4

Saturday, October 30, 2021

dbutils... ORMS .. awesome

 https://testsortedset.wordpress.com/javasnippets/my-tiny-mini-orm-with-commons-dbutils-and-generics-2/

Thursday, October 28, 2021

SSL or no SSL ?

 https://mkyong.com/webservices/jax-ws/java-security-cert-certificateexception-no-name-matching-localhost-found/

Friday, October 22, 2021

Fat runnable jars with maven

 First read this for the different ways:

https://www.baeldung.com/executable-jar-with-maven


If you have external/local jars, understand the problem:

https://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them


One possible dirty way:

https://github.com/stephenc/non-maven-jar-maven-plugin

Apache CXF and SOAP XML Logging

 Phew this one was tough..but managed to solve it eventually using the following:

https://newbedev.com/apache-cxf-loggingininterceptor-is-deprecated-what-to-use-instead

https://stackoverflow.com/questions/56356503/how-to-log-soap-request-response-using-log4j2-in-separate-log-files-dynamically

And this one did the most work

https://newbedev.com/how-to-modify-the-raw-xml-message-of-an-outbound-cxf-request


In between, had to mess with Streams.. it was crazy:

https://newbedev.com/convert-outputstream-to-bytearrayoutputstream

https://stackoverflow.com/questions/5923817/how-to-clone-an-inputstream/5924132#5924132

https://stackoverflow.com/questions/33014035/write-from-output-stream-to-output-stream


Finally .. learnt alot again.

Wednesday, July 7, 2021

Jar with dependencies using maven

 This works for me:

https://www.programmersought.com/article/82714095304/