我想在我的一个项目中使用 Atmosphere 并且在使用它来返回简单的 POJO 序列化为 JSONP 时遇到一些问题。我不明白 @Produces 注释和com.sun.jersey.api.json.JSONWithPadding
我之前成功使用的必要对象之间的关系,以便在简单的 RESTful 服务中序列化我的 POJO-s。
这是我的暂停方法:
@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
框架似乎想要序列化 Notification 对象但无法这样做。JsonpFilter
似乎很闲。我不确定此方法是否应该返回 Notification 或JSONWithPadding
包装对象。如果我从@Broadcast 注释中删除过滤器,那么挂起方法会发出一个“com.ericsson.nss.entities.Notification@308be6”字符串。这比异常更好,但仍然不是 JSONP 消息。不幸的是,从 maven repo 构建的最新 rest-chat 演示没有运行(其他人提到的 /chat 上的 404)。
如果我的广播方法返回一个JSONWithPadding
实例并且过滤器关闭,则广播请求会获得有效的 JSONP 响应,但暂停的线程会再次返回 com.ericsson.nss.entities.Notification@7f84c9。
你能告诉我如何正确使用过滤器和注释吗?
(我使用的是最新的 0.9 版本的 Atmosphere)