我正在尝试使用 Atmosphere 返回 JAXB 注释的 POJO 的 JSON 表示。
@Path("/entity/{topic}")
public class JerseyPubSub
{
@PathParam("topic")
private Broadcaster topic;
@GET
@Produces("application/json")
public SuspendResponse<String> subscribe()
{
return new SuspendResponse.SuspendResponseBuilder<String>()
.broadcaster(topic)
.outputComments(true)
.build();
}
@POST
@Broadcast
@Produces("application/json")
public Broadcastable publish(Notification notification)
{
return new Broadcastable(notification, topic);
}
}
我知道我的数据绑定(bind)工作正常,因为 POSTing JSON“通知”对象工作正常。
我是否可以使用 JAXB JSON 序列化将传递到“new Broadcastable()”的对象转换为 JSON 对象?
最佳答案
试试这段代码
@AtmosphereService(broadcaster=JerseyBroadcaster.class)
@Path("entity/{topic}")
public class WebService {
@PathParam(value="topic")
String topic;
@Context
private BroadcasterFactory factory;
@Suspend(contentType = "application/json", listeners = {OnDisconnect.class})
@GET
public Broadcastable suspend() {
return new Broadcastable( factory.lookup(topic, true ) );
}
@Broadcast(writeEntity = false)
@POST
@Produces("application/json")
public Response publish(Notification notification) {
return new Response(notification);
}
public static final class OnDisconnect extends AtmosphereResourceEventListenerAdapter {
private final Logger logger = LoggerFactory.getLogger(WebService.class);
@Override
public void onDisconnect(AtmosphereResourceEvent event) {
if (event.isCancelled()) {
logger.info("Browser {} unexpectedly disconnected", event.getResource().uuid());
} else if (event.isClosedByClient()) {
logger.info("Browser {} closed the connection", event.getResource().uuid());
}
}
}
}
希望这会有所帮助...:)
关于Java Atmosphere Jersey 从 Broadcastable 返回 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21409865/