简体   繁体   中英

convert openAPI specification to a Postman collection using java/spring boot

I`m trying to convert an OpenAPI specification to a postman collection using spring boot. So, is there a library or a code segment which I can use to do this task? I searched about this but I found none.

I did this earlier using an npm library. I'll put the code segment below.

var Converter = require('openapi-to-postmanv2'),
  openapiData = fileReader.result;

Converter.convert({ type: 'string', data: openapiData },
  {}, (err, conversionResult) => {

    if (!conversionResult.result) {
      console.log('Could not convert', conversionResult.reason);
    }
    else {
      console.log('The collection object is: ', conversionResult.output[0].data);
    }
  }
);

source: https://www.npmjs.com/package/openapi-to-postmanv2

I need help to do this using spring boot

In java you can run node script as a shell command and read output from it.

  1. First create new node project with npm init command.

  2. Create index.js file and add the following code. I have modified your code to get input from command line arguments instead of reading from a file.

var Converter = require('openapi-to-postmanv2')
openapiData = process.argv[2]

Converter.convert({ type: 'string', data: openapiData },
    {}, (err, conversionResult) => {
        if (!conversionResult.result) {
            console.log('Could not convert', conversionResult.reason);
        }
        else {
            console.log(conversionResult.output[0].data);
        }
    }
);
  1. Create App.java and add following lines.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class App {
    public static void main(String[] args) throws InterruptedException, IOException {
        String data = "YOUR_DATA_HERE";
        String command = String.format("node index.js \"%s\"", data);
        Runtime runtime = Runtime.getRuntime();
        Process pr = runtime.exec(command);
        pr.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String output = "";
        String line;
        while ((line = reader.readLine()) != null) {
            output += line;
        }
        System.out.println(output);
    }
}
  1. Compile and run it.
javac App.java
java App

Please note that this is a very minimalist example. You could use standard Error Stream to read errors in your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM