2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.