Easy Debugging!

Many a times one needs to debug an application by printing out the state of a java bean or multiple java beans with complex inner variables which may themselves be java beans or complex data types such as arraylists. This task is simplified a lot using the gson utility.

The following standalone application implements two java beans that print out their state by extending a base class that overrides Java Object class’ toString() method.

In the toString() method gson is used to print out formatted state of the java bean that inherits it.


package com.mattiz.bean;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Bean {
	public String toString() {
		Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting()
		return gson.toJson(this);


package com.mattiz.bean;

import java.util.ArrayList;
import java.util.List;

public class SampleBean extends Bean {
	private String name;
	private int id;
	private List names;
	private SampleBean2 sampleBean2 = new SampleBean2();

	public SampleBean2 getSampleBean2() {
		return sampleBean2;

	public void setSampleBean2(SampleBean2 sampleBean2) {
		this.sampleBean2 = sampleBean2;

	public SampleBean() {
		this.name = "SampleBean";
		this.id = 1;
		this.names = new ArrayList();


	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public int getId() {
		return id;

	public void setId(int id) {
		this.id = id;

	public List getNames() {
		return names;

	public void setNames(List names) {
		this.names = names;


package com.mattiz.bean;

public class SampleBean2 extends Bean {
	private String address1;
	private String address2;
	private String city;

	public String getAddress1() {
		return address1;

	public void setAddress1(String address1) {
		this.address1 = address1;

	public String getAddress2() {
		return address2;

	public void setAddress2(String address2) {
		this.address2 = address2;

	public String getCity() {
		return city;

	public void setCity(String city) {
		this.city = city;

The main class that tests this functionality:

package com.mattiz;

import com.mattiz.bean.SampleBean;
import com.mattiz.bean.SampleBean2;

public class TestJson_2_4 {
	public static void main(String[] args) {
		SampleBean2 sp2 = new SampleBean2();
		SampleBean sp = new SampleBean();
		sp2.setAddress1("MJ street");
		System.out.println("SampleBean : " + sp);
		System.out.println("SampleBean2 : " + sp2);

Output looks something like this:

SampleBean : {
"name": "SampleBean",
"id": 1,
"names": [
"sampleBean2": {
"address1": "MJ street",
"address2": null,
"city": null
SampleBean2 : {
"address1": "MJ street",
"address2": null,
"city": null

The only jar I am using is gson-1.7.1.jar which has this debug utility.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: