Moxy,JSON和Jersey 2.0不会反序列化纯字符串数组 [英] Moxy, JSON and Jersey 2.0 does not deserialize plain String array

查看:174
本文介绍了Moxy,JSON和Jersey 2.0不会反序列化纯字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的设置中,我将球衣2.0与MOXy配合使用,如球衣中所述文档.我完全依靠 自动发现功能",因此我不使用任何额外的配置或JAXB注释.

In my current setup I use Jersey 2.0 with MOXy as discribed in jersey docs. I rely completely on the "Auto-Discoverable Features", so I do not use any extra configuration or JAXB annotation.

我的任务是在服务器端反序列化字符串数组.客户端正在发送JSON消息:

My task is to deserialize an array of strings on the server side. The client is sending the JSON message:

["foo","bar"]

在服务器端,以下方法标头应将其反序列化:

And on the server side the following method header should deserialize it:

@POST
@Path("/stringArray")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response stringArray(List<String> stringList) {
   ...
}

问题: stringList的内容为{null,null},因此元素的数量始终是正确的,但是字符串设置为null.

Problem: The content of stringList is {null,null}, so the amount of elements is always correct, but the strings are set to null.

与小型包装器类相同的方法也可以正常工作.这里是班级:

The same with a small wrapper class is working. Here the class:

public static class Data {
   public List<String> stringList;
}

将方法签名更改为stringArray(Data data)并将JSON消息更改为:

Changing methode signature to stringArray(Data data) and changing the JSON message to:

{"stringList": ["foo","bar"]}

这两种方法之间有什么区别?如何使纯字符串数组正常工作?

What is the difference between the two approaches and how can I get the plain string array working?

更新: 通过@Blaise的答复解决了所描述的问题.但是,与序列化POJO列表紧密相关的问题仍然无法解决.讯息:

Update: The described problem is fixed by answer from @Blaise. But the closely related problem of serializing a List of POJOs does still not work. Message:

[org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: 找不到针对媒体类型= application/json的MessageBodyWriter, type = class java.util.ArrayList,genericType = class java.util.ArrayList.]

[org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.]

我发现的唯一解决方案是再次使用包含List ...的小型包装器类.

The only solution I found is again using a small wrapper class containing the List...

推荐答案

您看到的问题是由于 EclipseLink JAXB(MOXy) :

The issue you are seeing is due to a bug in EclipseLink JAXB (MOXy):

此错误已在EclipseLink 2.5.1和2.6.0流中修复.您可以从以下链接 2013年7月5日下载每晚构建的文件:

This bug has been fixed in the EclipseLink 2.5.1 and 2.6.0 streams. You can download a nightly build starting July 5, 2013 from the following link:

这篇关于Moxy,JSON和Jersey 2.0不会反序列化纯字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆