DTO Beans
The juneau-bean libraries contain 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 DTO beans are simply beans with fluent-style setters that allow you to quickly construct HTML fragments as Java objects. These objects 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 HtmlBuilder class is a utility class with predefined static methods that allow you to easily construct DTO bean instances in a minimal amount of code.
import static org.apache.juneau.bean.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>
HTML5 for more information.
ATOM
The Juneau ATOM feed DTO beans are simply beans with fluent-style setters. The following code shows a feed being created programmatically using the AtomBuilder class.
import static org.apache.juneau.bean.atom.AtomBuilder.*;
Feed feed =
feed("tag:juneau.apache.org", "Juneau ATOM specification", "2016-01-02T03:04:05Z")
.subtitle(text("html").text("Describes stuff 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("[Update: Juneau supports ATOM.]")
)
);
// 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);
Atom for more information.
Swagger
The Juneau Swagger DTO beans are simply beans with fluent-style setters that allow you to quickly construct Swagger documents as Java objects. These objects 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.
static import org.apache.juneau.bean.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();
Swagger for more information.
SwaggerUI
The SwaggerUI class is a DTO bean class for generating Swagger user interfaces from Swagger beans.
The PetStore
example described later provides an example of auto-generated Swagger JSON:
Using SwaggerUI, we're able to render that JSON as a Swagger user interface when the request is asking for HTML:
Swagger UI for more information.