Object to HTML dumper for Java

Ever had to debug an application that used some complex objects? Objects containing other objects containing other objects etc. Then you may know it is a tedious job to inspect these objects during your debug session.

That’s why I wrote this easy to use Java equivalent of PHP’s var_dump aka print_r, or Perl’s Data::Dumper (sort of).

What it does
It takes an object (any object, Collection, Map, null, primitive, array, enum, or any combination of previous objects) as its argument, and dumps the content of the object as a html page. Included in the package is a css file to style your page, so the output has easy to distinguish colors for text, numbers, null values, warnings, errors etc.

Screenshot
ObjectDumper screenshot

What you need

  • A super complicated, mind blowing, look how smart I am, object.
  • Dependency: objectdumper.
  • Other dependencies: commons-beanutils, commons-lang, commons-logging. (You’ll probably already have them).
  • A web browser.
  • A stylesheet (optional, but highly recommended)

Download
objectdumper.jar
Source files
Stylesheet

Configuration
The ObjectDumper class uses a default configuration that can be overridden by a .properties file.
The defaults are:

# The maximum number of levels ObjectDumper follows member objects.
# This is to prevent running out of memory, and because of circular
# references.

maxRecursionLevel=5

# Comma seperated list of packages the ObjectDumper should dump.
# Yes, you need to add java.util if you want Collections and Maps
# to be dumped.

dumpPackages=java.util

# Max items of arrays, Collections and Maps that need to be dumped.
maxItems=10
# Path where the css file can be found. Absolute or relative to the
# output html page.

stylesheetHref=dump.css

# If you want to incorporate the dump inside your own html page
# (I wanted that) then set this property to true (no html, head
# and body  declaration).

onlyHtmlContent=false

# Output full package+class name or only class name.

simpleClassNames=false

Usage
Inside a JSP page

<%
ObjectDumper dumper = new ObjectDumper(new StringWriter());
dumper.loadConfig(getServletContext().getRealPath("WEB-INF") + System.getProperty("file.separator") + "dump.properties"); 
%>
<%=dumper.writeObjectDump(anObject)%>

In a non web based Java application

ObjectDumper dumper = new ObjectDumper(new FileWriter("/somedir/test.html"));
dumper.loadConfig("/some_dir/dump.properties"); 
dumper.writeObjectDump(anObject);

Point your web browser to /somedir/test.html and happy debugging!

Comments 1

  1. shane smith wrote:

    You could also you apache commons lang ToStringBuilder, or even better ReflectionToStringBuilder:


    public String toString(){
    ReflectionToStringBuilder ref = new ReflectionToStringBuilder(this);
    return ref.toString();
    }

    http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/ToStringBuilder.html

    Posted 01 Oct 2008 at 02:07

Post a Comment

Your email is never published nor shared. Required fields are marked *