Reputation: 275
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
Upvotes: 2
Views: 1235
Reputation: 2852
In java you can run node script as a shell command and read output from it.
First create new node project with npm init
command.
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);
}
}
);
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);
}
}
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.
Upvotes: 1