我有Jersey PUT webservice,它接受包含多个对象数组的JSON / xml - 如下所示作为输入。
{
"a" : [{"p1":"x1"},{"p2":"x2"}],
"b" : [{"q1":"y1"},{"q2":"y2"}],
"c" : [{"r1":"z1"},{"r2":"z2"}]
}
在服务类中,这将转换为JavaBean输入参数。我已经验证了xsd映射是否正确。 问题是,当我使用JSON作为输入调用服务时,在服务器端javabean中,我看到只有正确填充的第一个值,但第二个和第三个值始终为null。 换句话说,如果我传递下面不同组合场景中列出的json,我会看到相应的结果。
{"a":[{}], "b":[{}], "c":[{}] }
只有" a"在JavaBean中收到。 " B"和" c"收到为null。
{"b":[{}], "c":[{}], "a":[{}] }
只有" b"在JavaBean中收到。 " C"和" a"收到为null。
{"c":[{}], "a":[{}], "b":[{}] }
只有" c"在JavaBean中收到。 ""和" b"收到为null。
因此,无论我从客户端传递的顺序如何,我似乎只能正确获得第一个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)服务方法PUT的输入POJO CreateABC.java如下:
//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)来自客户端/ fiddler工具的输入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 :(得分: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;
它有效。