views:

493

answers:

1

I have been trying to wrap my head around POSTing json to a REST service and JAXB namespaces. I am using Resteasy to mark up the server side method. I have the following service:

@POST
@Mapped(namespaceMap={@XmlNsMap(namespace="http://acme.com", jsonName=""))
@Path("/search")
@Consumes("application/json")
public List<I> search(SearchCriteria crit);

I have the following objects:

@XmlRootElement(namespace="http://acme.com")
public class DateCriteria {
    @XmlElement
    private Date start;
    @XmlElement
    private Date end;
}


@XmlRootElement(namespace="http://acme.com")
public class MultCriteria {
    @XmlElementRefs({@XmlElementRef(type=DateCriteria.class)})
    private List<DateCriteria> dateCriterias;
}

I can send the following JSON in a post and it works:

{ "acme.MultCriteria": { "acme.DateCriteria": { startDate: "2009/01/01", endDate: "2009/01/01" } } }

On the service, I get a MultCriteria object with a single element list of DateCriteria's. Strangely, I have to pass the namespace in the JSON object even though I marked the service with a map to the empty namespace.

If I try to send an array as follows:

{ "acme.MultCriteria": { "acme.DateCriteria": [ { startDate: "2009/01/01", endDate: "2009/01/01" }, { startDate: "2009/01/01", endDate: "2009/01/01" } ] } }

I get a MultCriteria object with an empty List of DateCriteria. If I modify the DateCriteria object so that it has an empty namespace, then the above syntax works fine.

Does anyone see what I am doing wrong here? How should the namespaces be setup and how do I properly pass them in to the service?

+1  A: 

In plain JAXB, when you annotate a class with @XmlRootElement(namespace="http://acme.com"), the namespace declaration does not automatically apply to all child elements. It has to be explicitly set on each field, e.g.

@XmlRootElement(namespace="http://acme.com")
public class MultCriteria {
    @XmlElement(name="DateCriteria", namespace="http://acme.com")
    private List<DateCriteria> dateCriterias;
}

As to how this interacts with RESTeasy, I can't say, I'm not familiar with the JSON-JAXB translation.

skaffman