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:

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


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 file with your Java client API. You extract this file, run "mvn install", then open the to understand how it works...

I create a Java project, add this maven dependency


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();
        String continuationToken = null;
  PageAssetXO assets = apiInstance.getAssets("maven-central", continuationToken );
  for (AssetXO asset :  assets.getItems()) {



actually you don't even need to run swagger-editor locally, you can use the online version

To test, populate your nexus repos as explained here

If you get a 403 Unauthorized, try assigning to the user "Anonymous" the role "nx2-repository-any-full" (=nx-repository-view-*-*-*)

in the request.log you see this - - [18/Dec/2018:17:57:01 +0100] "GET /service/rest/v1/assets?repository=central HTTP/1.1" 200 - 4698 328 "Swagger-Codegen/1.0.0/java"

Soem sample code is here

No comments: