Java将从Broadcastable返回JSON对象
我试图使用Atmosphere返回JAXB注释的POJO的JSON表示Java将从Broadcastable返回JSON对象,java,json,websocket,jersey,atmosphere,Java,Json,Websocket,Jersey,Atmosphere,我试图使用Atmosphere返回JAXB注释的POJO的JSON表示 @Path("/entity/{topic}") public class JerseyPubSub { @PathParam("topic") private Broadcaster topic; @GET @Produces("application/json") public SuspendResponse<String> subscribe() {
@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);
}
}
@Path(“/entity/{topic}”)
公共级运动衫
{
@PathParam(“主题”)
私营广播机构专题;
@得到
@生成(“应用程序/json”)
公共SuspendResponse订阅()
{
返回新的SuspendResponse.SuspendResponseBuilder()
.广播员(主题)
.outputComments(真)
.build();
}
@职位
@广播
@生成(“应用程序/json”)
公共可广播发布(通知)
{
返回新的可广播内容(通知、主题);
}
}
我知道我的数据绑定在发布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());
}
}
}
}
希望这将有助于……:)