noittom
5 years agoNew Contributor
Convert OpenAPI object into Json
Hi, I want to publish OpenAPI json via Rest by myself .
I managed to get openApi based on my Java code, now I =need to get json - how to do it ?
i.e.
OpenAPI oas = new OpenAPI();
Info info = new Info()
.title("Swagge...")
.description("This is ..")
.termsOfService("http://swagger.io/terms/")
.contact(new Contact()
.email("apiteam@swagger.io"))
.license(new License()
.name("Apache 2.0")
.url("htt..tml"));
oas.info(info);
SwaggerConfiguration oasConfig = new SwaggerConfiguration()
.openAPI(oas)
.resourcePackages(Stream.of("cpm.example.my.package").collect(Collectors.toSet()));
OpenApiContext openApiContext =new JaxrsOpenApiContextBuilder()
.openApiConfiguration(oasConfig)
.buildContext(true);
openApiContext.read();
now openApiContext has all the info - how to get it as JSON ?
(one of the reason I am asking is that I didnt manage swagger core to publish my open API spec (I use spring and not JAX and I do not want to use springfox )
apparently you can do it like this:
Json.prettyPrint(o);
so in total there is a nice way to get openAPI for your code in your java program