2013-10-24 68 views
0

我收到提示com.kp.schema.Value」被代"java.lang.Object", but "com.kp.schema.Value" is bound to an anonymous type..代java.lang.Object繼承綁定到一個匿名類型

我使用CXF JSONProvider試圖元帥Java對象的JSONObject,我希望JSON對象爲

{ 
    "KpJSON": 
    { 
     "header": 
     { 
      "cmd_resource": "commd", 
      "cmd_action": "PUT", 
     }, 

     "parameters": 
     [ 
      { 
       "name": "p1", 
       "syntax": "string", 
       "value": "text1" 
      }, 
      { 
       "name": "p2", 
       "syntax": "integer", 
       "value": 1 
      }, 
      { 
       "name": "input", 
       "syntax": "container", 
       "value": 
       [ 
        { 
         "name": "ch1", 
         "syntax": "string", 
         "value": "file" 
        }, 
        { 
         "name": "ch2", 
         "syntax": "boolean", 
         "value": false 
        } 
       ] 
      } 
     ] 
    } 
} 

我paramters.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "name", 
    "syntax", 
    "value" 
}) 
@XmlRootElement(name = "parameters") 
public class Parameters { 

    @XmlElement(required = true) 
    protected String name; 
    @XmlElement(required = true) 
    protected SyntaxType syntax; 
    @XmlElement(required = true) 
    protected List<Value> value; 

    //getters and setters 

}

Value.java

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "name", 
    "syntax", 
    "value" 
}) 
@XmlRootElement(name = "value") 
public class Value { 

    @XmlElement(required = true) 
     protected String name; 
     @XmlElement(required = true) 
     protected SyntaxType syntax; 
     @XmlElement(required = true) 
     protected List<Value> value; 

     //getters and setters 

} 

當我試圖改變列表類型來價值Paramters.java和改性Value.java到以下

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "value") 
public class Value { 

    @XmlElementRefs({ 
     @XmlElementRef(name = "name", namespace = "http://kp.com/schema/", type = JAXBElement.class), 
     @XmlElementRef(name = "syntax", namespace = "http://kp.com/schema/", type = JAXBElement.class), 
     @XmlElementRef(name = "value", namespace = "http://kp.com/schema/", type = Value.class) 
    }) 
    @XmlMixed 
    protected List<Object> content; 

// getters and setters 

} 

並試圖創建一個對象,如下所示,輸出是空的用於容器。

Parameters parameters3 = new Parameters(); 

parameters3.setName("K3"); 
parameters3.setSyntax(SyntaxType.CONTAINER); 

Value val2 = new Value(); 
val2.getContent().add("sdfsdfs"); 
QName qname= new QName(""); 
JAXBElement kpsyntax = new JAXBElement(qname, SyntaxType.class, SyntaxType.CONTAINER); 

kpsyntax.setValue(SyntaxType.CONTAINER); 

Value val3 = new Value(); 
val2.getContent().add(kpsyntax); 
parameters3.getValue().add(val3); 


response.setHeader(header); 
response.getParameters().add(parameters3); 

JSON輸出是:

{"KpJSON":{"header":{"cmd_resource":"command","cmd_action":"PUT"},"parameters":{"name":"K3","syntax":"container","value":""}}} 

有人可以幫我解決這個問題或任何建議

+0

你有兩個'class Value',哪一個是'com.kp.schema.Value'? – Ilya

回答

0

我使用第二種方法解決了這一問題。由於@llya詢問哪個JSON提供者有類似的問題,因此需要循環引用。因此我修改了Value類如下。

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "value") 

    public class Value { 

     @XmlElementRefs({ 
      @XmlElementRef(name = "syntax", namespace = "http://kp.com/schema/", type = JAXBElement.class), 
      @XmlElementRef(name = "nodeValue", namespace = "http://kp.com/schema/", type = NodeValue.class), 
      @XmlElementRef(name = "name", namespace = "http://kp.com/schema/", type = JAXBElement.class) 
     }) 
     @XmlMixed 
     @XmlAnyElement(lax = true) 
     protected List<Object> content; 

創造的nodeValue類

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "value") 
public class NodeValue { 

    @XmlElementRefs({ 
     @XmlElementRef(name = "syntax", namespace = "http://kp.com/schema/", type = JAXBElement.class), 
     @XmlElementRef(name = "value", namespace = "http://kp.com/schema/", type = NodeValue.class), 
     @XmlElementRef(name = "name", namespace = "http://kp.com/schema/", type = JAXBElement.class) 
    }) 
    @XmlMixed 
    @XmlAnyElement(lax = true) 
    protected List<Object> content; 

而且我這是怎麼形成的響應。

Parameters parameters3 = new Parameters(); 

     parameters3.setName("K3"); 
     parameters3.setSyntax(SyntaxType.CONTAINER); 
     Value paramvalue3 = obj.createValue(); 

     JAXBElement<String> name = obj.createName("ch1"); 
     JAXBElement<SyntaxType> synt = obj.createSyntax(SyntaxType.STRING); 
     NodeValue ch1Value = obj.createNodeValue(); 

     ch1Value.getContent().add("KPStringChild1");  
     paramvalue3.getContent().add(name); 
     paramvalue3.getContent().add(synt); 
     paramvalue3.getContent().add(ch1Value); 
     parameters3.getValue().add(paramvalue3); 
相關問題