我收到错误com.kp.schema.Value“代替"java.lang.Object", but "com.kp.schema.Value" is bound to an anonymous type..
我正在尝试使用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 :(得分:0)
我使用第二种方法修复了问题。由于@llya在询问哪个com.kp.schema.Value
JSON Provider有类似问题,因此需要进行循环引用。因此我按如下方式修改了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);