1

我收到错误消息:

com.sun.istack.SAXException2:“com.kp.schema.Value”的实例正在替换“java.lang.Object”,但“com.kp.schema.Value”绑定到匿名类型。

我正在尝试使用 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
                    }
                ]
            }  
        ]
    }
}

我的参数.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
}

价值.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
}

当我尝试将 List 类型更改为 Value inParamters.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":""}}}
4

1 回答 1

0

我使用第二种方法解决了这个问题。当@llya 询问哪个com.kp.schema.ValueJSON 提供程序有类似问题时,它正在循环引用。因此,我修改了 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);
于 2013-10-27T10:59:42.293 回答