1 Reply Latest reply on Sep 7, 2011 7:26 AM by kiranshirali

    How to return an empty tag for a null value in RestEasy ?

    kiranshirali

      I have a POJO that has the following structure:

       

      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlRootElement;

       

      @XmlRootElement(name = "Item")
      public class Item implements java.io.Serializable
      {
      private String attribute1;
          private String attribute2;
         
          @XmlElement
      public String getAttribute1() {
        return attribute1;
      }
         
          @XmlElement
      public String getAttribute2() {
        return attribute2;
      }
      public void setAttribute1(String attribute1) {
        this.attribute1 = attribute1;
      }
      public void setAttribute2(String attribute2) {
        this.attribute2 = attribute2;
      }
         
      }

       

       

      Now on a webservice call, the XML returned to the client will have the structure:

       

      <Item>

            <attribute1>Some_Value</attribute1>

            <attribute2>Some_Value</attribute2>

      </Item>

       

       

      However suppose any of the attributes are null, then the returned XML does not have that particular attribute tag.

       

      For example, if attribute2 is null then the XML structure is:

       

      <Item>

            <attribute1>Some_Value</attribute1>

      </Item>

       

      My requirement is that the attribute tag should remain as an empty tag. The structure should be like:

       

      <Item>

            <attribute1>Some_Value</attribute1>

            <attribute2></attribute2>

      </Item>

       

      Is this possible in RestEasy ? What are the changes that I will have to do to implement this? Please can someone help me on this.

        • 1. Re: How to return an empty tag for a null value in RestEasy ?
          kiranshirali

          I have found a solution to this.

           

          If you have an object that is being returned, then make sure that the object is instanciated.

           

          For example:

           

          If you are returning an object Employee. Then by doing a new Employee() and returning it, we get the following xml tag:

           

          <Employee/>

           

          Simlarly if Employee was a string then ensure that a empty string ("") is being returned and not null. It gives the same effect as above.