Skip to content

Instantly share code, notes, and snippets.

@jmini
Created July 16, 2020 05:24
Show Gist options
  • Save jmini/0ab9b3335a07fdc073e6668835636ba9 to your computer and use it in GitHub Desktop.
Save jmini/0ab9b3335a07fdc073e6668835636ba9 to your computer and use it in GitHub Desktop.
Write an OpenAPI spec with Java code (instead of YAML or JSON directly)
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS io.smallrye:smallrye-open-api-core:2.0.3
import static org.eclipse.microprofile.openapi.OASFactory.*;
import org.eclipse.microprofile.openapi.models.OpenAPI;
import io.smallrye.openapi.runtime.io.Format;
import io.smallrye.openapi.runtime.io.OpenApiSerializer;
public class example {
public static void main(String... args) throws Exception {
OpenAPI spec = createSpec();
String content = OpenApiSerializer.serialize(spec, Format.YAML);
System.out.println(content);
}
public static OpenAPI createSpec() {
return createOpenAPI()
.openapi("3.0.1")
.info(
createInfo()
.title("Ping Specification")
.version("1.0")
)
.addServer(
createServer()
.url("http://localhost:8080/")
)
.paths(
createPaths()
.addPathItem(
"/ping", createPathItem()
.GET(
createOperation()
.operationId("pingGet")
.responses(
createAPIResponses()
.addAPIResponse(
"200", createAPIResponse()
.description("OK")
)
)
)
)
);
}
}
@jmini
Copy link
Author

jmini commented Jul 17, 2020

See the corresponding article:
Write your OpenAPI with Java instead of YAML

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment