Swagger UI

The {@link oaj.dto.swagger.ui.SwaggerUI} class is a DTO class for generating Swagger user interfaces from {@link oaj.dto.swagger.Swagger} beans.

The PetStore example described later provides an example of auto-generated Swagger JSON:

Using {@link oaj.dto.swagger.ui.SwaggerUI}, we're able to render that JSON as a Swagger user interface when the request is asking for HTML:

The class itself is nothing more than a POJO swap that swaps out {@link oaj.dto.swagger.Swagger} beans with {@link oaj.dto.html5.Div} elements:

public class SwaggerUI extends PojoSwap<Swagger,Div> { @Override public MediaType[] forMediaTypes() { // Only use this swap when the Accept type is HTML. return new MediaType[] {MediaType.HTML}; } @Override public Div swap(BeanSession beanSession, Swagger swagger) throws Exception { ... } }

The {@link oajr.BasicRestServlet} class (describe later) shows how this swap is used in the REST interface to generate the Swagger UI shown above:

@Rest( // Allow OPTIONS requests to be simulated using ?method=OPTIONS query parameter. allowedMethodParams="OPTIONS", ... ) @BeanConfig( // POJO swaps to apply to all serializers/parsers. swaps={ // Use the SwaggerUI swap when rendering Swagger beans. SwaggerUI.class } ) public abstract class BasicRestServlet extends RestServlet implements BasicRestConfig { /** * [OPTIONS /*] - Show resource options. */ @RestMethod( name=OPTIONS, path="/*", summary="Swagger documentation", description="Swagger documentation for this resource." ) @HtmlDocConfig( // Override the nav links for the swagger page. navlinks={ "back: servlet:/", "json: servlet:/?method=OPTIONS&Accept=text/json&plainText=true" }, // Never show aside contents of page inherited from class. aside="NONE" ) public Swagger getOptions(RestRequest req) { // Localized Swagger for this resource is available through the RestRequest object. return req.getSwagger(); } }