BasicRestServlet/BasicRestObject Swagger
Any subclass of BasicRestServlet and BasicRestObject gets an auto-generated Swagger UI when performing an OPTIONS
request with Accept:text/html
due to the following method:
@RestGet(
path="/api/*",
summary="Swagger documentation",
description="Swagger documentation for this resource."
)
@HtmlDocConfig(
// Should override config annotations defined on class.
rank=10,
// Override the nav links for the swagger page.
navlinks={
"back: servlet:/",
"json: servlet:/?Accept=text/json&plainText=true"
},
// Never show aside contents of page inherited from class.
aside="NONE"
)
@BeanConfig(
// POJO swaps to apply to all serializers/parsers on this method.
swaps={
// Use the SwaggerUI swap when rendering Swagger beans.
// This is a per-media-type swap that only applies to text/html requests.
SwaggerUI.class
}
)
@Override /* BasicRestOperations */
public Swagger getSwagger(RestRequest req) {
return req.getSwagger().orElseThrow(NotFound::new);
}
The underlying mechanics are simple. The BasicRestServlet.getSwagger(RestRequest) method returns a Swagger bean consisting of information gathered from annotations and other sources. Then that bean is swapped for a SwaggerUI bean when rendered as HTML.
Note that to have your resource create Swagger UI, you must either extend from one of the basic resource classes or provide your own @RestOp-annotated method that returns a Swagger object and a SwaggerUI swap.