Thursday, May 17, 2018

Swagger Editor and generation of a Java API for REST Nexus API

first, let's run a docker container for swagger editor:

https://hub.docker.com/r/swaggerapi/swagger-editor/

docker run -d --rm --name swaggereditor -p 9080:8080 swaggerapi/swagger-editor

http://localhost:9080/


then http://localhost:8181/service/rest/swagger.json (8181 is the Nexus port) , download the file, feed it into the swagger-editor (file , import file). Import URL will not work, apparently you must be authenticated in Nexus to be able to download it.

then generate client /java , this will produce a java-client-generated.zip file with your Java client API. You extract this file, run "mvn install", then open the README.md to understand how it works...

I create a Java project, add this maven dependency

<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-java-client</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>


then try this code:

package nexusclient;

import io.swagger.client.*;
import io.swagger.client.auth.*;
import io.swagger.client.model.*;
import io.swagger.client.api.AssetsApi;

public class NexucClientTest {
 public static void main(String[] args) throws Exception {
  AssetsApi apiInstance = new AssetsApi();
  apiInstance.getApiClient().setBasePath("http://localhost:8181/service/rest");
        String continuationToken = null;
  PageAssetXO assets = apiInstance.getAssets("maven-central", continuationToken );
  for (AssetXO asset :  assets.getItems()) {
   System.out.println(asset.toString());
  }

 }

}




actually you don't even need to run swagger-editor locally, you can use the online version https://editor.swagger.io//?_ga=2.252946753.972179590.1526550450-1850471649.1526550450#/



To test, populate your nexus repos as explained here http://www.javamonamour.org/2017/09/nexus-and-maven-setup.html






No comments: