{title:'DTOs', created:'9.0.0'}

The juneau-dto library contains several predefined POJOs for generating commonly-used document types that are designed to be used with the Juneau Marshaller APIs for both serializing and parsing.

HTML5

The Juneau HTML5 DTOs are simply beans with fluent-style setters that allow you to quickly construct HTML fragments as Java objects. These object can then be serialized to HTML using one of the existing HTML serializers, or to other languages such as JSON using the JSON serializers.

The {@link oaj.dto.html5.HtmlBuilder} class is a utility class with predefined static methods that allow you to easily construct DTO instances in a minimal amount of code.

Examples:

| import static org.apache.juneau.dto.html5.HtmlBuilder.*; | | // An HTML table | Object mytable = | table( | tr( | th("c1"), | th("c2") | ), | tr( | td("v1"), | td("v2") | ) | ); | | String html = Html.of(mytable);

| <table> | <tr> | <th>c1</th> | <th>c2</th> | </tr> | <tr> | <td>v1</td> | <td>v2</td> | </tr> | </table>

ATOM

The Juneau ATOM feed DTOs are simply beans with fluent-style setters. The following code shows a feed being created programmatically using the {@link oaj.dto.atom.AtomBuilder} class.

Example:

| import static org.apache.juneau.dto.atom.AtomBuilder.*; | | Feed feed = | feed("tag:juneau.apache.org", "Juneau ATOM specification", "2016-01-02T03:04:05Z") | .subtitle(text("html").text("Describes <em>stuff</em> about Juneau")) | .links( | link("alternate", "text/html", "http://juneau.apache.org").hreflang("en"), | link("self", "application/atom+xml", "http://juneau.apache.org/feed.atom") | ) | .rights("Copyright (c) ...") | .generator( | generator("Juneau").uri("http://juneau.apache.org/").version("1.0") | ) | .entries( | entry("tag:juneau.sample.com,2013:1.2345", "Juneau ATOM specification snapshot", "2016-01-02T03:04:05Z") | .links( | link"alternate", "text/html", "http://juneau.apache.org/juneau.atom"), | link("enclosure", "audio/mpeg", "http://juneau.apache.org/audio/juneau_podcast.mp3").length(1337) | ) | .published("2016-01-02T03:04:05Z") | .authors( | person("Jane Smith").uri("http://juneau.apache.org/").email("janesmith@apache.org") | ) | .contributors( | person("John Smith") | ) | .content( | content("xhtml") | .lang("en") | .base("http://www.apache.org/") | .text("<div><p><i>[Update: Juneau supports ATOM.]</i></p></div>") | ) | ); | | // Create a serializer with readable output, no namespaces yet. | XmlSerializer serializer = XmlSerializer.create().sq().ws().build(); | | // Serialize to ATOM/XML | String atomXml = serializer.serialize(feed);

Swagger

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.

Example:

| static import org.apache.juneau.dto.swagger.SwaggerBuilder.*; | | Swagger swagger = swagger() | .swagger("2.0") | .info( | info("Swagger Petstore", "1.0.0") | .description("This is a sample server Petstore server.") | .termsOfService("http://swagger.io/terms/") | .contact( | contact().email("apiteam@swagger.io") | ) | .license( | license("Apache 2.0").url("http://www.apache.org/licenses/LICENSE-2.0.html") | ) | ) | .host("petstore.swagger.io") | .basePath("/v2") | .tags( | tag("pet").description("Everything about your Pets") | .externalDocs( | externalDocumentation("http://swagger.io", "http://swagger.io") | ) | ) | .schemes("http") | .path("/pet", "post", | operation() | .tags("pet") | .summary("Add a new pet to the store") | .description("") | .operationId("addPet") | .consumes(MediaType.JSON, MediaType.XML) | .produces(MediaType.JSON, MediaType.XML) | .parameters( | parameterInfo("body", "body") | .description("Pet object that needs to be added to the store") | .required(true) | ) | .response(405, responseInfo("Invalid input")) | ); | | // Serialize using JSON serializer. | String swaggerJson = Json.of(swagger); | | // Or just use toString() or asJson(). | String swaggerJson = swagger.asJson();

SwaggerUI

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: