The Juneau Swagger DTOs are simply beans with fluent-style setters that allow you to quickly construct
Swagger documents as Java objects.
These object can then be serialized to JSON using one of the existing JSON serializers, or to other
languages such as XML or HTML using the other serializers.
The {@link oaj.dto.swagger.SwaggerBuilder} class is a utility class with predefined static
methods that allow you to easily construct DTO instances in a minimal amount of code.
The following is an example Swagger document from the
Swagger website.
| {
| "swagger": "2.0",
| "info": {
| "title": "Swagger Petstore",
| "description": "This is a sample server Petstore server.",
| "version": "1.0.0",
| "termsOfService": "http://swagger.io/terms/",
| "contact": {
| "email": "apiteam@swagger.io"
| },
| "license": {
| "name": "Apache 2.0",
| "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
| }
| },
| "host": "petstore.swagger.io",
| "basePath": "/v2",
| "tags": [
| {
| "name": "pet",
| "description": "Everything about your Pets",
| "externalDocs": {
| "description": "Find out more",
| "url": "http://swagger.io"
| }
| }
| ],
| "schemes": [
| "http"
| ],
| "paths": {
| "/pet": {
| "post": {
| "tags": [
| "pet"
| ],
| "summary": "Add a new pet to the store",
| "description": "",
| "operationId": "addPet",
| "consumes": [
| "application/json",
| "text/xml"
| ],
| "produces": [
| "application/json",
| "text/xml"
| ],
| "parameters": [
| {
| "in": "body",
| "name": "body",
| "description": "Pet object that needs to be added to the store",
| "required": true
| }
| ],
| "responses": {
| "405": {
| "description": "Invalid input"
| }
| }
| }
| }
| }
| }
This document can be generated by the following Java code:
| static import org.apache.juneau.dto.swagger.SwaggerBuilder.*;
|
| Swagger swagger = swagger()
| .setSwagger("2.0")
| .setInfo(
| info("Swagger Petstore", "1.0.0")
| .setDescription("This is a sample server Petstore server.")
| .setTermsOfService("http://swagger.io/terms/")
| .setContact(
| contact().setEmail("apiteam@swagger.io")
| )
| .setLicense(
| license("Apache 2.0").setUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
| )
| )
| .setHost("petstore.swagger.io")
| .setBasePath("/v2")
| .setTags(
| tag("pet").setDescription("Everything about your Pets")
| .setExternalDocs(
| externalDocumentation("http://swagger.io", "http://swagger.io")
| )
| )
| .setSchemes("http")
| .setPath("/pet", "post",
| operation()
| .setTags("pet")
| .setSummary("Add a new pet to the store")
| .setDescription("")
| .setOperationId("addPet")
| .setConsumes(MediaType.JSON, MediaType.XML)
| .setProduces(MediaType.JSON, MediaType.XML)
| .setParameters(
| parameterInfo("body", "body")
| .setDescription("Pet object that needs to be added to the store")
| .setRequired(true)
| )
| .setResponse(405, responseInfo("Invalid input"))
| );
|
| // Serialize using JSON serializer.
| String swaggerJson = Json.of(swagger);
|
| // Or just use toString().
| String swaggerJson = swagger.toString();
Methods that take in beans and collections of beans can also take in JSON representations
of those objects.
| // Pass in a JSON object representation of an Info object.
| swagger.info("{title:'Swagger Petstore',...}");
Properties can also be accessed via the {@link oaj.dto.swagger.SwaggerElement#get(String,Class)}
and {@link oaj.dto.swagger.SwaggerElement#set(String,Object)} methods.
These methods can also be used to set and retrieve non-Swagger attributes such as
"$ref" (which is not a part of the Swagger spec but is part of the JSON Schema spec).
| // Set a non-standard attribute.
| swagger.set("$ref", "http://foo.com");
|
| // Retrieve a non-standard attribute.
| URI ref = swagger.get("$ref", URI.class);
Swagger docs can be parsed back into Swagger beans using the following code:
| Swagger swagger = JsonParser.DEFAULT.parse(swaggerJson, Swagger.class);