Class HtmlSerializer

All Implemented Interfaces:
AnnotationProvider, HtmlMetaProvider, XmlMetaProvider
Direct Known Subclasses:
HtmlSchemaSerializer, HtmlSerializer.Sq, HtmlSerializer.SqReadable, HtmlStrippedDocSerializer

public class HtmlSerializer extends XmlSerializer implements HtmlMetaProvider
Serializes POJO models to HTML.
Media types

Handles Accept types: text/html

Produces Content-Type types: text/html

Description

The conversion is as follows...

  • Maps (e.g. HashMap, TreeMap) and beans are converted to HTML tables with 'key' and 'value' columns.
  • Collections (e.g. HashSet, LinkedList) and Java arrays are converted to HTML ordered lists.
  • Collections of Maps and beans are converted to HTML tables with keys as headers.
  • Everything else is converted to text.

This serializer provides several serialization options. Typically, one of the predefined DEFAULT serializers will be sufficient. However, custom serializers can be constructed to fine-tune behavior.

The HtmlLink annotation can be used on beans to add hyperlinks to the output.

Behavior-specific subclasses

The following direct subclasses are provided for convenience:

Example:

// Use one of the default serializers to serialize a POJO String html = HtmlSerializer.DEFAULT.serialize(someObject); // Create a custom serializer that doesn't use whitespace and newlines HtmlSerializer serializer = HtmlSerializer.create().ws().build(); // Same as above, except uses cloning HtmlSerializer serializer = HtmlSerializer.DEFAULT.copy().ws().build(); // Serialize POJOs to HTML // Produces: // <ul><li>1<li>2<li>3</ul> List list = JsonList.of(1, 2, 3); String html = HtmlSerializer.DEFAULT.serialize(list); // Produces: // <table> // <tr><th>firstName</th><th>lastName</th></tr> // <tr><td>Bob</td><td>Costas</td></tr> // <tr><td>Billy</td><td>TheKid</td></tr> // <tr><td>Barney</td><td>Miller</td></tr> // </table> html = JsonList.of( JsonMap.ofJson("{firstName:'Bob',lastName:'Costas'}"), JsonMap.ofJson("{firstName:'Billy',lastName:'TheKid'}"), JsonMap.ofJson("{firstName:'Barney',lastName:'Miller'}") ); String html = HtmlSerializer.DEFAULT.serialize(list); // Produces: // <table> // <tr><th>key</th><th>value</th></tr> // <tr><td>foo</td><td>bar</td></tr> // <tr><td>baz</td><td>123</td></tr> // </table> Map map = JsonMap.ofJson("{foo:'bar',baz:123}"); String html = HtmlSerializer.DEFAULT.serialize(map); // HTML elements can be nested arbitrarily deep // Produces: // <table> // <tr><th>key</th><th>value</th></tr> // <tr><td>foo</td><td>bar</td></tr> // <tr><td>baz</td><td>123</td></tr> // <tr><td>someNumbers</td><td><ul><li>1<li>2<li>3</ul></td></tr> // <tr><td>someSubMap</td><td> // <table> // <tr><th>key</th><th>value</th></tr> // <tr><td>a</td><td>b</td></tr> // </table> // </td></tr> // </table> Map map = JsonMap.ofJson("{foo:'bar',baz:123}"); map.put("someNumbers", JsonList.of(1, 2, 3)); map.put("someSubMap", JsonMap.ofJson("{a:'b'}")); String html = HtmlSerializer.DEFAULT.serialize(map);

Notes:
  • This class is thread safe and reusable.
See Also: