Created
July 16, 2020 05:24
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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") | |
) | |
) | |
) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the corresponding article:
Write your OpenAPI with Java instead of YAML