使用 Atmosphere 返回 JSONP 序列化对象
我想在我的一个项目中使用 Atmosphere,但在使用它返回序列化为 JSONP 的简单 POJO 时遇到一些问题。我不明白 @Produces 注释和我之前在简单的 RESTful 服务中成功使用的 com.sun.jersey.api.json.JSONWithPadding 对象之间的关系。
这是我的挂起方法:
@GET
@Path("/notification")
@Produces( { "application/x-javascript", MediaType.APPLICATION_JSON })
@Suspend
public JSONWithPadding getNextNotification(
@QueryParam("callback") @DefaultValue("callback") String callback) {
Random random = new Random();
Notification n = new Notification();
n.setMessage("Message is " + Long.toHexString(random.nextLong()));
n.setMessage("S-" + Long.toHexString(random.nextLong()));
return new JSONWithPadding(n, callback);
}
这会按预期返回适当的 JSON 字符串。问题来了。我有返回的广播方法:
@Broadcast({XSSHtmlFilter.class, JsonpFilter.class})
@GET
@Path("/broadcast2")
public Notification broadcast2() {
Random random = new Random();
Notification n = new Notification();
n.setMessage("Message is " + Long.toHexString(random.nextLong()));
n.setMessage("S-" + Long.toHexString(random.nextLong()));
return n;
}
这会产生以下异常:
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.ericsson.nss.entities.Notificaion, and Java type class com.ericsson.nss.entities.Notification, and MIME media type application/octet-stream was not fund
框架似乎想要序列化通知对象,但无法这样做。 JsonpFilter
似乎闲置。我不确定此方法是否应该返回通知或 JSONWithPadding
包装对象。如果我从 @Broadcast 注释中删除过滤器,则挂起方法会发出“com.ericsson.nss.entities.Notification@308be6”字符串。这比异常更好,但仍然不是 JSONP 消息。不幸的是,从maven repo构建的最新rest-chat演示无法运行(正如其他人提到的/chat上的404)。
如果我的广播方法返回 JSONWithPadding 实例并且过滤器关闭,则广播请求将获得有效的 JSONP 响应,但挂起的线程会再次返回 com.ericsson.nss.entities.Notification@7f84c9。
你能告诉我如何正确使用过滤器和注释吗?
(我使用的是Atmosphere最新的0.9版本)
I would like to use Atmosphere in one of my projects and have some problems using it to return simple POJO-s serialized into JSONP. I do not understand the relation between the @Produces annotation and the necessary com.sun.jersey.api.json.JSONWithPadding
object that I used successfully before to serialize my POJO-s in a simple RESTful service.
Here is my suspending method:
@GET
@Path("/notification")
@Produces( { "application/x-javascript", MediaType.APPLICATION_JSON })
@Suspend
public JSONWithPadding getNextNotification(
@QueryParam("callback") @DefaultValue("callback") String callback) {
Random random = new Random();
Notification n = new Notification();
n.setMessage("Message is " + Long.toHexString(random.nextLong()));
n.setMessage("S-" + Long.toHexString(random.nextLong()));
return new JSONWithPadding(n, callback);
}
This returns the appropriate JSON string to me as expected. And here comes the problem. I have the broadcaster method that returns:
@Broadcast({XSSHtmlFilter.class, JsonpFilter.class})
@GET
@Path("/broadcast2")
public Notification broadcast2() {
Random random = new Random();
Notification n = new Notification();
n.setMessage("Message is " + Long.toHexString(random.nextLong()));
n.setMessage("S-" + Long.toHexString(random.nextLong()));
return n;
}
This produces the following exception:
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class com.ericsson.nss.entities.Notificaion, and Java type class com.ericsson.nss.entities.Notification, and MIME media type application/octet-stream was not fund
It seems that the framework would like to serialize the Notification object but is unable to do so. The JsonpFilter
seems idle. I'm not sure if this method should return Notification or a JSONWithPadding
wrapping object. If I remove the filters from the @Broadcast annotation, then the suspending method emits a "com.ericsson.nss.entities.Notification@308be6" string. This is nicer then the exception, but still not a JSONP message. Unfortunately the latest rest-chat demo built from maven repo is not operating (404 on /chat as mentioned by others).
If my broadcast method returns a JSONWithPadding
instance and filters are off, then the broadcast request gets a valid JSONP response but the suspended thread returns com.ericsson.nss.entities.Notification@7f84c9 again.
Can you tell me how to use filters and annotations correctly?
(I am using the latest 0.9 version of Atmosphere)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只能通过从我的方法返回 String 实例并使用 Jackson 手动处理 JSONP 序列化来使其工作。
I could only make it work by returning
String
instances from my methods and handle JSONP serialization manually using Jackson.