2016-02-13 214 views
0

我有Jersey PUT webservice,它採用包含多個對象數組的JSON/xml(如下所示)作爲輸入。嵌套在JSON對象中的嵌套數組屬性爲null

{ 
"a" : [{"p1":"x1"},{"p2":"x2"}], 
"b" : [{"q1":"y1"},{"q2":"y2"}], 
"c" : [{"r1":"z1"},{"r2":"z2"}] 
} 

在服務類中,它被轉換爲JavaBean輸入參數。我已經驗證了xsd映射是正確的。 問題是,當我使用JSON作爲輸入調用服務時,在服務器端javabean中,我只看到第一個值正確填充,但第二個和第三個值始終爲空。 換句話說,如果我通過了下面不同組合場景中列出的json,我會看到如上所述的相應結果。

  1. {"a":[{}], "b":[{}], "c":[{}] }
    只有 「A」 進行的JavaBean接收。 「b」和「c」被接收爲空。

  2. {"b":[{}], "c":[{}], "a":[{}] }
    在JavaBean中只接收到「b」。 「c」和「a」被接收爲空。

  3. {"c":[{}], "a":[{}], "b":[{}] }
    在JavaBean中只接收到「c」。 「a」和「b」被接收爲空。

因此,看起來我只是得到正確的第一個JSON鍵值對,無論我以什麼順序從客戶端傳遞。

感謝您對此的幫助。

更多細節如下: 這是遺留系統,我沒有權利分享的實際代碼。系統工作正常,只需輸入一個數組,並且需要更新,因此它可以接受JSON中的多個數組,如解釋的問題。

以下是系統中完全不同的部分。另外我已經驗證了所有的xml名稱空間和POJO屬性等都是正確的,並且註釋得當。

1),它實現PUT方法的CreateABCResource.java:

//package x.y.z 
//import javax.ws.rs.*..etc; 

@Path("mypath") 
public class CreateABCResource{ 

    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
    @PUT 
    public AResponse doSomething(CreateABC createABC) { 
    AResponse x = new AResponse(); 
    //business logic here 
    return x; 
    } 
} 

2)輸入POJO CreateABC.java到服務方法PUT是如下:

//package com.x.y 
//import ...; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "CreateABC", namespace = "urn:x", propOrder = { 
    "abc","def","pqr" 
}) 
@XmlRootElement(name = "createAbc", namespace = "urn:x") 
public class CreateABC { 

    @XmlElement(name = "abc", namespace = "urn:x") 
    protected List<Abc> abc = null; 
    @XmlElement(name = "def", namespace = "urn:x") 
    protected List<Def> def = null; 
    @XmlElement(name = "pqr", namespace = "urn:x") 
    protected List<Pqr> pqr = null; 

    @XmlAttribute(required = true) 
    protected String a; 
    @XmlAttribute(required = true) 
    protected String b; 
    @XmlAttribute(required = true) 
    protected String c; 
    //and so on ... 

    //getter setters...  
} 

3)輸入JSON來自客戶端/提琴手測試工具:

{ 
    "a": "some value a", 
    "b": "some value b", 
    "c": "some value c", 
    "abc":[{},{}], 
    "def":[{},{}], 
    "pqr":[{},{}] 
} 

4)包含JAXB映射的xsd:

<xs:element name="createAbc" type="CreateABC" /> 
    <xs:complexType name="CreateABC"> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="abc" type="Abc" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="def" type="Def" /> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="pqr" type="Pqr" /> 
    </xs:sequence> 
     <!-- THIS IS COMMENTED, BUT THIS ALSO DIDNOT WORK IN PLACE OF ABOVE sequence 
     <xs:all> 
      <xs:element minOccurs="0" maxOccurs="unbounded" name="abc" type="Abc" /> 
      <xs:element minOccurs="0" maxOccurs="unbounded" name="def" type="Def" /> 
      <xs:element minOccurs="0" maxOccurs="unbounded" name="pqr" type="Pqr" /> 
     </xs:all> 
     --> 
    <xs:attribute name="a" use="required" type="xs:string" /> 
    <xs:attribute name="b" use="required" type="xs:string" /> 
    <xs:attribute name="c" use="required" type="xs:string" /> 
    </xs:complexType> 

    <xs:complexType name="Abc"> 
    <!-- CORRET DEFINITON OF Abc is present here --> 
    </xs:complexType> 
    <xs:complexType name="Def"> 
    <!-- CORRET DEFINITON OF Def is present here --> 
    </xs:complexType>  
    <xs:complexType name="Pqr"> 
    <!-- CORRET DEFINITON OF Pqr is present here --> 
    </xs:complexType> 
+0

json不使用xsd,我認爲你必須與xml在這裏混淆 – gerrytan

+0

你是對的@gerrytan xsd是否存在只是因爲該服務也支持xml,我已經在這方面提及它。 – Nitin

+0

您使用什麼庫和Java類來反序列化Json? – gerrytan

回答

1

替換下面的代碼輸入POJO:

@XmlElement(name = "abc", namespace = "urn:x") 
protected List<Abc> abc = null; 
@XmlElement(name = "def", namespace = "urn:x") 
protected List<Def> def = null; 
@XmlElement(name = "pqr", namespace = "urn:x") 
protected List<Pqr> pqr = null; 

有:

protected List<Abc> abc = null; 
protected List<Def> def = null; 
protected List<Pqr> pqr = null; 

和它的作品。