通过抛弃/Resteasy解组JSON数组
遇到类似问题,如以下论坛帖子: 将Resteasy 2.0.1GA与抛弃1.2结合使用,并在涉及命名空间映射时编组数组时遇到问题。请参阅下面的代码。基本上,如果数组项的数量大于1,并且使用了名称空间映射,那么就可以使用。还有其他人遇到这个问题吗?Nabble表单海报通过编写自定义解组器绕过了它 我要么需要隔离抛弃bug,要么编写抛弃nmappedunmarshaller类的Resteasy扩展(它将名称空间映射和解组器交给抛弃配置) 如果properties变量包含2个或多个条目,则以下代码不会解组(步骤后)通过抛弃/Resteasy解组JSON数组,json,resteasy,jettison,Json,Resteasy,Jettison,遇到类似问题,如以下论坛帖子: 将Resteasy 2.0.1GA与抛弃1.2结合使用,并在涉及命名空间映射时编组数组时遇到问题。请参阅下面的代码。基本上,如果数组项的数量大于1,并且使用了名称空间映射,那么就可以使用。还有其他人遇到这个问题吗?Nabble表单海报通过编写自定义解组器绕过了它 我要么需要隔离抛弃bug,要么编写抛弃nmappedunmarshaller类的Resteasy扩展(它将名称空间映射和解组器交给抛弃配置) 如果properties变量包含2个或多个条目,则以下代码不
public class Experimenting {
@Path("test")
public static class MyResource {
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Property", propOrder = { "name", "value" })
public static class MyProperty {
@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Value", required = true)
protected String value;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
@XmlType(name = "MyElement", propOrder = { "myProperty" })
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MyElement", namespace = "http://www.klistret.com/cmdb/ci/commons")
@Mapped(namespaceMap = { @XmlNsMap(namespace = "http://www.klistret.com/cmdb/ci/commons", jsonName = "com.klistret.cmdb.ci.commons") })
public static class MyElement {
@XmlElement(name = "MyProperty", namespace = "http://www.klistret.com/cmdb/ci/commons")
protected List myProperty;
public List getMyProperty() {
if (myProperty == null) {
myProperty = new ArrayList();
}
return this.myProperty;
}
public void setMyProperty(List myProperty) {
this.myProperty = myProperty;
}
}
@GET
@Path("myElement/{id}")
@Produces(MediaType.APPLICATION_JSON)
public MyElement getMy(@PathParam("id")
Long id) {
MyElement myElement = new MyElement();
MyProperty example = new MyProperty();
example.setName("example");
example.setValue("of a property");
MyProperty another = new MyProperty();
another.setName("another");
another.setValue("just a test");
MyProperty[] properties = new MyProperty[] { example, another };
myElement.setMyProperty(Arrays.asList(properties));
return myElement;
}
@POST
@Path("/myElement")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MyElement createMy(MyElement myElement) {
List properties = myElement.getMyProperty();
System.out.println("Properties size: " + properties.size());
return myElement;
}
}
private Dispatcher dispatcher;
@Before
public void setUp() throws Exception {
// embedded server
dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addPerRequestResource(MyResource.class);
}
@Test
public void getAndCreate() throws URISyntaxException,
UnsupportedEncodingException {
MockHttpRequest getRequest = MockHttpRequest.get("/test/element/44");
MockHttpResponse getResponse = new MockHttpResponse();
dispatcher.invoke(getRequest, getResponse);
String getResponseBodyAsString = getResponse.getContentAsString();
System.out.println(String.format(
"Get Response code [%s] with payload [%s]", getResponse
.getStatus(), getResponse.getContentAsString()));
MockHttpRequest postRequest = MockHttpRequest.post("/test/element");
MockHttpResponse postResponse = new MockHttpResponse();
postRequest.contentType(MediaType.APPLICATION_JSON);
postRequest.content(getResponseBodyAsString.getBytes("UTF-8"));
dispatcher.invoke(postRequest, postResponse);
System.out.println(String.format(
"Post Response code [%s] with payload [%s]", postResponse
.getStatus(), postResponse.getContentAsString()));
}
}
你必须使用抛弃吗?如果不是的话,我建议改为使用;这通常可以解决与数组/列表相关的问题(抛弃的问题是它转换为XML模型,这使得很难区分数组和对象——也有bug,但从根本上说很难正常工作).在Get return和Post参数/return上使用BadgerFish可以很好地工作。底层注释是JAXB,而我所做的少量阅读中的Jackson只支持JAXB注释的一个子集。切换到Badgerfish解决了上述问题,但遇到了字符串数组解组问题。:-)可以在本地使用Resteasy模拟类marhsall/unmarshall字符串数组,但通过服务器,完全相同的JSON会产生字符串解析器错误。Jackson几乎支持所有有意义的注释——许多JAXB注释都是特定于XML的,不适用。但只使用了不支持,我听说有人希望使用XmlID。因此,这通常没有问题。此外,大多数情况下,不需要注释(Jackson有自己的集合,支持JAXB,但由于xml的特殊性,实际上并不推荐使用JAXB)。但如果獾鱼管用,那就好了。这很难看,但大家都有自己的想法