users@jersey.java.net

[Jersey] XML & JSON namespace PROBLEM

From: Jairo1 <jairo_at_azet.sk>
Date: Sat, 7 May 2011 17:38:05 -0700 (PDT)

Hello Jakub (or whoever could help)

I'm facing with the following problem.
REST web server provides me a response on my POST request in JSON or XML
format.Using ClientResponse approach I'm checking the type of the response:

ACCEPT_MEDIA_TYPES -> both json & xml accepted
ClientResponse response =
server.path("malls").type(MediaType.APPLICATION_JSON).accept(ACCEPT_MEDIA_TYPES).post(ClientResponse.class);

//get response media type (XML or JSON)
      responseType = response.getType();

Based on the response type I'm using JSONJAXBContext & JSONUnmarshaller to
deserialize JSON data or JAXBContext & Unmarshaller to deserialize XML data.

(hope this is correct approach...)

Based on the:
String textEntity = (String)response.getEntity(String.class);
data in both formats are received correctly.


The PROBLEM is with unmarshaling these following beans:
**************

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "mall",
    "shopDetail"
})
@XmlRootElement(name = "mallDetail")
public class MallDetail {

    @XmlElement(required = true)
    protected Mall mall;
    @XmlElement(required = true)
    protected List<ShopDetail> shopDetail;

    public Mall getMall() {
        return mall;
    }

    public void setMall(Mall value) {
        this.mall = value;
    }

    public List<ShopDetail> getShopDetail() {
        if (shopDetail == null) {
            shopDetail = new ArrayList<ShopDetail>();
        }
        return this.shopDetail;
    }
}

***********
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "shop",
    "preferredFood",
    "food"
})
@XmlRootElement(name = "shopDetail")
public class ShopDetail {

    @XmlElement(required = true)
    protected Shop shop;
    @XmlElement(required = true)
    @XmlSchemaType(name = "unsignedLong")
    protected BigInteger preferredFood;
    @XmlElement(required = true)
    protected List<Food> food;

    public Shop getShop() {
        return shop;
    }

    public void setShop(Shop value) {
        this.shop = value;
    }

    public BigInteger getPreferredFood() {
        return preferredFood;
    }

    public void setPreferredFood(BigInteger value) {
        this.preferredFood = value;
    }

    public List<Food> getFood() {
        if (food == null) {
            food = new ArrayList<Food>();
        }
        return this.food;
    }

}

From POST response I get MallDetail containing
1.Mall mall
2.List<ShopDetail> shopDetail


The problem is that when the namespace = "http://example.com/malls" is just
in the package-info.java, only JSON deserializing works fine.
XML unmarshalling returns :
1.Mall bean of MallDetail is correct
2.List<ShopDetail> of MallDetail is not empty but 'Shop' and 'List<Food>'
are null.
 

And vice versa. If I add namespace annotations also to 'ShopDetail' bean:
...
    @XmlElement(namespace = "http://example.com/malls", required = true)
    protected Shop shop;
    @XmlElement(required = true)
    @XmlSchemaType(name = "unsignedLong")
    protected BigInteger preferredFood;
    @XmlElement(namespace = "http://example.com/malls", required = true)
    protected List<Food> food;
...
and MallDetail
   @XmlElement(namespace = "http://example.com/malls", required = true)
    protected Mall mall;
    @XmlElement(namespace = "http://example.com/malls", required = true)
    protected List<ShopDetail> shopDetail;


XML response is not null, but JSON unmarshalling returns NULL for 'Shop' and
'List<Food>'


What could be wrong or what is missing regarding namespace annotations ?
Is it even possible to handle both data types JSON&XML using the same
annotated JAXB-beans and stringEntity generated from ClientResponse class?


NOTE: Meantime I have found out that direct
MallDetail entity = (MallDetail )response.getEntity(MallDetail .class);
works fine for JSON&XML response (using namespace annotations). But there
is still open question why this doesn't work using stringEntity + JSON and
XML unmarshalling.

Thanks for your advice.
Jairo





--
View this message in context: http://jersey.576304.n2.nabble.com/XML-JSON-namespace-PROBLEM-tp6341028p6341028.html
Sent from the Jersey mailing list archive at Nabble.com.