Search code examples
xmljaxbmappingjavabeanspojo

How to create Plain Old Java Object for Database (RESTful service design)?


I would like to generate POJO, XML for a given database.

Database: (contents is the table name)

enter image description here

Interested in knowing how POJO will look like and corresponding XML for the same.

For instance (xml look like this)

<?xml version="1.0" encoding="UTF-8" ?>
<contents>
  <param1>x</param1>
  <param2>y</param2>
  <param3>2</param3>
</contents>

& corresponding POJO

public class Contents
{
    @XmlElement
    String param1;

    @XmlElement
    String param2;

    @XmlElement
    String param3;

    public String getparam1()
    {
        return param1;
    }

    public void setParam1( String param1 )
    {
        this.param1 = param1;
    }

    public String getparam2()
    {
        return param2;
    }

    public void setParam2( String param2 )
    {
        this.param2= param2;
    }

    public String getparam3()
    {
        return param3;
    }

    public void setParam3( String param3)
    {
        this.param3= param3;
    }

}

So if i want to add multiple ROWS ( 2nd and 3rd row ) in my XML how does my POJO and XML will look ?

assuming (xml should be)

<?xml version="1.0" encoding="UTF-8" ?>
<contents>
 <content>
   <param1>x</param1>
   <param2>y</param2>
   <param3>2</param3>
 </content>
 <content>
   <param1>a</param1>
   <param2>b</param2>
   <param3>1</param3>
 </content>
</contents>

Solution

  • I hope this will work for you.

    @XmlRootElement(name="contents")
    public class ContentsWrapper {
    
        public Collection<Contents> getContents() {
            if (contents == null) {
                contents = new ArrayList<Contents>();
            }
            return contents;
        }
    
        @XmlElement(name="content")
        private Collection<Contents> contents;
    }