users@jersey.java.net

[Jersey] json conversion results in empty object

From: Peter De Koster <peter.de.koster_at_gmail.com>
Date: Mon, 10 Jan 2011 00:01:53 +0100

Hello,

I'm using a class that was generated from an xsd using sun's maven plugin.
 Now I have to use it for a JAX-RS / JERSEY resource in json format.


Trying this I don't get actual exceptions but the object is completely empty
after the conversion (so all fields are null), whereas the same application
is using other jaxb annotated classes in resources with json.


This class does look strange (it is generated).

Does anyone see anything special about this class:


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "status",
    "expiryDate",
    "parentIdTag"
})
@XmlRootElement(name = "authorize.conf")
public class AuthorizeConf
    implements Serializable, Equals, HashCode, ToString
{

    private final static long serialVersionUID = 2L;
    @XmlElement(required = true)
    protected AuthorizationStatus status;
    @XmlElement(required = true, type = String.class)
    @XmlJavaTypeAdapter(DateAdapter.class)
    @XmlSchemaType(name = "date")
    protected Date expiryDate;
    protected String parentIdTag;

    /**
     * Gets the value of the status property.
     *
     * @return
     * possible object is
     * {_at_link AuthorizationStatus }
     *
     */
    public AuthorizationStatus getStatus() {
        return status;
    }

    /**
     * Sets the value of the status property.
     *
     * @param value
     * allowed object is
     * {_at_link AuthorizationStatus }
     *
     */
    public void setStatus(AuthorizationStatus value) {
        this.status = value;
    }

    /**
     * Gets the value of the expiryDate property.
     *
     * @return
     * possible object is
     * {_at_link String }
     *
     */
    public Date getExpiryDate() {
        return expiryDate;
    }

    /**
     * Sets the value of the expiryDate property.
     *
     * @param value
     * allowed object is
     * {_at_link String }
     *
     */
    public void setExpiryDate(Date value) {
        this.expiryDate = value;
    }

    /**
     * Gets the value of the parentIdTag property.
     *
     * @return
     * possible object is
     * {_at_link String }
     *
     */
    public String getParentIdTag() {
        return parentIdTag;
    }

    /**
     * Sets the value of the parentIdTag property.
     *
     * @param value
     * allowed object is
     * {_at_link String }
     *
     */
    public void setParentIdTag(String value) {
        this.parentIdTag = value;
    }

    public void toString(ToStringBuilder toStringBuilder) {
        {
            AuthorizationStatus theStatus;
            theStatus = this.getStatus();
            toStringBuilder.append("status", theStatus);
        }
        {
            Date theExpiryDate;
            theExpiryDate = this.getExpiryDate();
            toStringBuilder.append("expiryDate", theExpiryDate);
        }
        {
            String theParentIdTag;
            theParentIdTag = this.getParentIdTag();
            toStringBuilder.append("parentIdTag", theParentIdTag);
        }
    }

    public String toString() {
        final ToStringBuilder toStringBuilder = new
JAXBToStringBuilder(this);
        toString(toStringBuilder);
        return toStringBuilder.toString();
    }

    public void equals(Object object, EqualsBuilder equalsBuilder) {
        if (!(object instanceof AuthorizeConf)) {
            equalsBuilder.appendSuper(false);
            return ;
        }
        if (this == object) {
            return ;
        }
        final AuthorizeConf that = ((AuthorizeConf) object);
        equalsBuilder.append(this.getStatus(), that.getStatus());
        equalsBuilder.append(this.getExpiryDate(), that.getExpiryDate());
        equalsBuilder.append(this.getParentIdTag(), that.getParentIdTag());
    }

    public boolean equals(Object object) {
        if (!(object instanceof AuthorizeConf)) {
            return false;
        }
        if (this == object) {
            return true;
        }
        final EqualsBuilder equalsBuilder = new JAXBEqualsBuilder();
        equals(object, equalsBuilder);
        return equalsBuilder.isEquals();
    }

    public void hashCode(HashCodeBuilder hashCodeBuilder) {
        hashCodeBuilder.append(this.getStatus());
        hashCodeBuilder.append(this.getExpiryDate());
        hashCodeBuilder.append(this.getParentIdTag());
    }

    public int hashCode() {
        final HashCodeBuilder hashCodeBuilder = new JAXBHashCodeBuilder();
        hashCode(hashCodeBuilder);
        return hashCodeBuilder.toHashCode();
    }

    public AuthorizeConf withStatus(AuthorizationStatus value) {
        setStatus(value);
        return this;
    }

    public AuthorizeConf withExpiryDate(Date value) {
        setExpiryDate(value);
        return this;
    }

    public AuthorizeConf withParentIdTag(String value) {
        setParentIdTag(value);
        return this;
    }

}

I'm having this problem in my unit test in which I perform something like:

webresource.type(APPLICATION_JSON_TYPE).accept(APPLICATION_JSON_TYPE)
        .put(AuthorizeConf.class, req);


Now what's strange here is that the "req" object is of type AuthorizeReq, is
generated in the same way and has the exact same problem. But I solved it
adding a @Provider class (just using natural, so I don't know why it helps),
this only gets me past reading the req object. The authorizeconf object is
still wrong. What's wrong with this class? Or does the empty object problem
ring any bell in general ?

The problem occurs with jersey 1.1.5 & 1.4 . Normal Jaxb marshalling &
unmarshalling of these objects works.

-- 
Please think about our environment before considering printing this mail.