سؤال

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)

هل كانت مفيدة؟

المحلول

I could only make it work by returning String instances from my methods and handle JSONP serialization manually using Jackson.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top