Construct HTML fragments Atom feeds Swagger documents using Apache Juneau

Ayeshmantha Perera
Dec 15, 2018 · 3 min read

What is Apache Juneau

Apache JUNEAU

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

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

2. Java Library

juneau-dto-7.2.2.jar

3.OSGi Module

org.apache.juneau.dto_7.2.2.jar

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

  • HTML5
  • Atom
  • Cognos
  • JSON-Schema
  • Swagger 2.0

Usage of HTML5 DTOs to generate HTML5 documents and fragments.

import static org.apache.juneau.dto.html5.HtmlBuilder.*;Object mainJsp =
       form().action("main.jsp").method("GET")
       .children(
         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);

Outputs

<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>
</form>

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);

Produces

{
  a:{action:'main.jsp',method:'GET'},
  c:[
     {a:{type:'text',name:'first_name',value:'apache'}},{},
     {a:{type:'text',name:'last_name',value:'juneau'}},{},
     {a:{type:'submit'},c:['Submit']},
     {a:{type:'reset'},c:['Reset']}
  ]
}

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("tag:juneau.apache.org", "Juneau ATOM specification", "2018-12-15T08:52:05Z")
     .title("Example apache Juneau feed")
     .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.")
     )
     .rights("Copyright (c) 2016, Apache Foundation")
     .authors(new Person("Juneau_Commiter"))
     .updated("2018-12-15T08:52:05Z")
     .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>")
       )
);

Produces

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

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()
 .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);

Produces

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

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.

References

Ayeshmantha Perera

Written by

#apache #opensource