Class OpenApiClientGenerator
@RestClient interface (annotations and all) from an
OpenAPI 3.x document - the opposite direction of this library's own
compile-time proxy generation, which turns a hand-written interface into
an implementation. Spec-first instead of annotation-first: point this at
an existing API's OpenAPI JSON and get a ready-to-compile interface
instead of transcribing every path/parameter by hand.
Scope: this is a skeleton generator, not a full schema-to-POJO code
generator like swagger-codegen/OpenAPI Generator - every parameter and
every request/response body is generated as String. OpenAPI's own
{name} path-template placeholder syntax already matches
@PathParam's
exactly, so paths need no translation at all. What this generator gets
right - the paths, HTTP methods, parameter names/locations, and base URL -
is the tedious, error-prone part of a large API; narrowing a specific
parameter's type or replacing a body's String with your own POJO
is a normal hand-edit of the generated file afterward, not something this
generator attempts.
Only get/post/put/delete/patch
operations are read from each path item (head/options/
trace are skipped); a requestBody of any shape becomes one
@Body String parameter; every parameters entry with
in: path or in: query becomes a
@PathParam/@QueryParam of type String (any other
in - header/cookie - is skipped, since honoring it
is a per-call/interceptor concern, not part of the interface shape).
servers[0].url, if present, becomes the interface's
@BaseUrl; its absence isn't an error - the generated interface
then simply requires every method's own URL to already be absolute,
exactly like a hand-written one would.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidReadsspecFileas an OpenAPI 3.x JSON document and writes a generated@RestClientinterface to<outputDirectory>/<interfaceName>.java, creatingoutputDirectory(and any missing parent directories) if needed.static voidCommand-line entry point:specFile outputDirectory packageName interfaceName.
-
Method Details
-
generate
public static void generate(File specFile, File outputDirectory, String packageName, String interfaceName) throws IOException ReadsspecFileas an OpenAPI 3.x JSON document and writes a generated@RestClientinterface to<outputDirectory>/<interfaceName>.java, creatingoutputDirectory(and any missing parent directories) if needed.- Parameters:
specFile- the OpenAPI 3.x document, as JSON (not YAML - convert first if your spec is YAML)outputDirectory- the directory the generated.javafile is written intopackageName- the generated interface's packageinterfaceName- the generated interface's simple name- Throws:
IOException- ifspecFilecan't be read or the output file can't be written
-
main
Command-line entry point:specFile outputDirectory packageName interfaceName.- Parameters:
args- the four positional arguments described above- Throws:
IOException- ifspecFilecan't be read or the output file can't be written
-