我收到错误消息:
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":""}}}