juneau-dto
Maven Dependency

<dependency> <groupId>org.apache.juneau</groupId> <artifactId>juneau-dto</artifactId> <version>7.0.0</version> </dependency>

Java Library

juneau-dto-7.0.0.jar

OSGi Module

org.apache.juneau.dto_7.0.0.jar

Data Transfer Object libraries are provided for a variety of languages that allow you to serialize commonly-used documents.

HTML5 documents and fragments can be constructed using the HTML5 DTOs and HTML or XML serializers:

import static org.apache.juneau.dto.html5.HtmlBuilder.*; Object myform = form().action("/submit").method("POST") .children( "Position (1-10000): ", input("number").name("pos").value(1), br(), "Limit (1-10000): ", input("number").name("limit").value(100), br(), button("submit", "Submit"), button("reset", "Reset") ); String html = HtmlSerializer.DEFAULT.serialize(myform);

<form action='/submit' method='POST'> Position (1-10000): <input name='pos' type='number' value='1'/><br/> Limit (1-10000): <input name='pos' type='number' value='100'/><br/> <button type='submit'>Submit</button> <button type='reset'>Reset</button> </form>

And you're not limited to just HTML. The HTML5 beans are POJOs that can be serialized using any of the serializers, such as lax JSON:

{ _type: 'form', a: { action: '/submit', method: 'POST' }, c: [ 'Position (1-10000): ', { _type: 'input', a: { type: 'number', name: 'pos', value: 1 } }, { _type: 'br' }, 'Limit (1-10000): ', { _type: 'input', a: { type: 'number', name: 'limit', value: 100 } }, { _type: 'br' }, { _type: 'button', a: { type: 'submit' }, c: [ 'Submit' ] }, { _type: 'button', a: { type: 'reset' }, c: [ 'Reset' ] } ] }

ATOM feeds can be constructed using the ATOM DTOs and XML serializer:

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) 2016, Apache Foundation") .entries( entry("tag:juneau.sample.com,2013:1.2345", "Juneau ATOM specification snapshot", "2016-01-02T03:04:05Z") .published("2016-01-02T03:04:05Z") .content( content("xhtml") .lang("en") .base("http://www.apache.org/") .text("<div><p><i>[Update: Juneau supports ATOM.]</i></p></div>") ) ); // Serialize to ATOM/XML String atomXml = XmlSerializer.DEFAULT.serialize(feed);

<feed> <id> tag:juneau.apache.org </id> <link href='http://juneau.apache.org/' rel='alternate' type='text/html' hreflang='en'/> <link href='http://juneau.apache.org/feed.atom' rel='self' type='application/atom+xml'/> <rights> Copyright (c) 2016, Apache Foundation </rights> <title type='text'> Juneau ATOM specification </title> <updated>2016-01-02T03:04:05Z</updated> <subtitle type='html'> Describes <em>stuff</em> about Juneau </subtitle> <entry> <id> tag:juneau.apache.org </id> <title> Juneau ATOM specification snapshot </title> <updated>2016-01-02T03:04:05Z</updated> <content base='http://www.apache.org/' lang='en' type='xhtml'> <div xmlns="http://www.w3.org/1999/xhtml"><p><i>[Update: Juneau supports ATOM.]</i></p></div> </content> <published>2016-01-02T03:04:05Z</published> </entry> </feed>

Swagger documents can be constructed using the Swagger DTOs and JSON serializer:

import static 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") ) ) .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 to Swagger/JSON String swaggerJson = JsonSerializer.DEFAULT_READABLE.serialize(swagger);

{ "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" } }, "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" } } } } }, }

Note that these DTOs can also be serialized to any of the other supported languages such as JSON or MessagePack! And they can be parsed back into their original objects!

As a convenience, you can also simply call toString() on any of these DTOs and they will be serialized directly to a string in the typical language (e.g. HTML5 beans to HTML, Swagger to JSON, etc...).