Construct HTML fragments Atom feeds Swagger documents using Apache Juneau

Ayeshmantha Perera
Dec 15, 2018 · 3 min read

What is Apache Juneau

Image for post
Image for post

Apache Juneau is a set of open source modules for marshaling POJOs to a wide variety of content types using a common framework, and for creating sophisticated self-documenting REST interfaces and microservices quickly and with very little code.Modules of the single cohesive Java ecosystem can be find here

As the definition says the module Apache Juneau MARSHALL is handling core process on marshaling POJOS.A tech guide on this module can be find here,

I will be focusing about the another core module in Apache Juneau which used construct Html fragments as Java objects.This is call as juneau-dto.

This module is a separate core module which can be used as a separate juneau module on you’re Java projects.It has 3 ways introduced to integrate with you’re java projects.

1.Maven Dependency


2. Java Library


3.OSGi Module


It has provided several libraries that allows you to serialize commonly used documents.

  • HTML5

Usage of HTML5 DTOs to generate HTML5 documents and fragments.

import static org.apache.juneau.dto.html5.HtmlBuilder.*;Object mainJsp =
input("text").name("first_name").value("apache"), br(),
input("text").name("last_name").value("juneau"), br(),
button("submit", "Submit"),
button("reset", "Reset")
String html = HtmlSerializer.DEFAULT.serialize(mainJsp);


<form action="main.jsp" method="GET">
<input type="text" name="first_name" value="apache"/><br/>
<input type="text" name="last_name" value="juneau"/><br/>
<button type="submit">Submit</button>
<button type="reset">Reset</button>

And not only converting to HTML5 documents.It can serialize HTML5 beans using JsonSerializers to clean JSON objects.

String html =  JsonSerializer.create().simple().sq().build().serialize(mainJsp);



With the use of ATOM DTOs and XML serializer Atom feeds which are developed according to Atom standards can be generated.All Atom feeds that generates are well-formed XML documents with Atom predefined element types.

import static org.apache.juneau.dto.atom.AtomBuilder.*;
Feed feed =
feed("", "Juneau ATOM specification", "2018-12-15T08:52:05Z")
.title("Example apache Juneau feed")
.subtitle(text("html").text("Describes <em>stuff</em> about Juneau"))
link("alternate", "text/html", "")
link("self", "application/atom+xml","http://juneau.apache.")
.rights("Copyright (c) 2016, Apache Foundation")
.authors(new Person("Juneau_Commiter"))
entry(",2013:1.2345", "Juneau ATOM specification snapshot", "2016-01-02T03:04:05Z")
.text("<div><p><i>[Update: Juneau supports ATOM.]</i></p></div>")


<title>Example apache Juneau feed</title>
<link href="" hreflang="en" rel="alternate" type="text/html"/>
<link href="" rel="self" type="application/atom+xml"/>
<rights>Copyright (c) 2016, Apache Foundation</rights>
<subtitle type="html">Describes <em>stuff</em> about Juneau</subtitle>
<title>Juneau ATOM specification snapshot</title>
<content lang="en" base="" type="xhtml"> <div><p><i>[Update: Juneau supports ATOM.]</i></p></div></content>

And another use case is generating Swagger documents.

The following is an example Swagger document from the Swagger website.

import static org.apache.juneau.dto.swagger.SwaggerBuilder.*;
Swagger swagger = swagger()
info("Swagger Petstore", "1.0.0")
.description("This is a sample server Petstore server.")
license("Apache 2.0").url("")
.path("/pet", "post",
.summary("Add a new pet to the store")
.consumes(MediaType.JSON, MediaType.XML)
.produces(MediaType.JSON, MediaType.XML)
parameterInfo("body", "body")
.description("Pet object that needs to be added to the store")
.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": "",
"contact": {
"email": ""
"license": {
"name": "Apache 2.0",
"url": ""
"paths": {
"/pet": {
"post": {
"tags": [
"summary": "Add a new pet to the store",
"description": "",
"operationId": "addPet",
"consumes": [
"produces": [
"parameters": [
"in": "body",
"name": "body",
"description": "Pet object that needs to be added to the store",
"required": true
"responses": {
"405": {
"description": "Invalid input"

Like wise juneau-dto module will help you on Constructing Html5 , Swagger and many more.This is a single core module in Apache Juneau.Tech world giants like IBM and Salesforce are using Apache Juneau because of the ability it has as a cohesive framework that allows developers to marshal POJOs (Plain Old Java Objects) and develop REST (Representational State Transfer) microservices and APIs..Since the project is opensource any one can contribute to improve it.


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store