从Jersey返回JSONP似乎不起作用

时间:2016-01-17 17:17:03

标签: java jersey jax-rs jsonp jersey-test-framework

我使用的是Jersey 2.22.1,看起来很简单。我已经有了返回json的端点,现在我希望它们返回jsonp。似乎所有我要做的就是使用@JSONP进行注释,但是当我测试时,我没有看到回调包装。

来自文档:

@GET
@JSONP
@Produces({"application/json", "application/javascript"})
public JaxbBean getSimpleJSONP() {
    return new JaxbBean("jsonp");
}

我的代码:

@GET
@Path("/jsonp")
@JSONP
@Produces({"application/json", "application/javascript"})
public Response getHeathJsonp() {
    return Response.ok().entity(ApplicationStatus.getNew("HEALTHY", "Application is healthy!")).build();
}

我的jquery电话:

   $.ajax({
      url: "http://foo.com/api/health/jsonp",
      dataType: 'jsonp',
      type: 'GET',
      data: "",
      success: function(data) {
        console.log(data);
      },
      error: function(xhr, status, err) {
        console.log(err.toString());
      }
    });

我的回答:

{
  "dateTime": {
    "monthOfYear": 1,
    "dayOfMonth": 17,
    "year": 2016,
    "dayOfWeek": 7,
    "era": 1,
    "dayOfYear": 17,
    "centuryOfEra": 20,
    "yearOfEra": 2016,
    "yearOfCentury": 16,
    "weekOfWeekyear": 2,
    "hourOfDay": 12,
    "minuteOfHour": 39,
    "secondOfMinute": 2,
    "millisOfSecond": 666,
    "millisOfDay": 45542666,
    "weekyear": 2016,
    "secondOfDay": 45542,
    "minuteOfDay": 759,
    "chronology": {
      "zone": {
        "uncachedZone": {
          "cachable": true,
          "fixed": false,
          "id": "America/New_York"
        },
        "fixed": false,
        "id": "America/New_York"
      }
    },
    "zone": {
      "uncachedZone": {
        "cachable": true,
        "fixed": false,
        "id": "America/New_York"
      },
      "fixed": false,
      "id": "America/New_York"
    },
    "millis": 1453052342666,
    "afterNow": false,
    "equalNow": true,
    "beforeNow": false
  },
  "description": "Application is healthy!",
  "status": "HEALTHY"
}

我的ApplicationStatus bean: 包com.foo.model.application;

import org.joda.time.DateTime;

public class ApplicationStatus {

    private String _status;
    private String _description;
    private DateTime _dateTime;

    private ApplicationStatus() {   }

    public static ApplicationStatus getNew(String status, String desc) {
        ApplicationStatus stat = new ApplicationStatus();
        stat.setDateTime(DateTime.now());
        stat.setStatus(status);
        stat.setDescription(desc);
        return stat;
    }

    public String getStatus() {
        return _status;
    }

    public void setStatus(String _status) {
        this._status = _status;
    }

    public String getDescription() {
        return _description;
    }

    public void setDescription(String _description) {
        this._description = _description;
    }

    public DateTime getDateTime() {
        return _dateTime;
    }

    public void setDateTime(DateTime _dateTime) {
        this._dateTime = _dateTime;
    }

}

项目依赖项:

dependencies {
    compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'

    compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.22.1'
    compile 'org.glassfish.jersey.core:jersey-server:2.22.1'
    compile 'org.glassfish.jersey.core:jersey-common:2.22.1'
    compile 'org.glassfish.jersey.core:jersey-client:2.22.1'

    compile 'org.glassfish.jersey.media:jersey-media-json-processing:2.22.1'
    compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.22.1'
    compile 'org.glassfish.jersey.media:jersey-media-multipart:2.22.1'
    compile 'org.glassfish.jersey.media:jersey-media-sse:2.22.1'

    compile 'com.yammer.metrics:metrics-graphite:2.2.0'
    compile 'com.google.guava:guava:18.0'
    compile 'joda-time:joda-time:2.9'

    compile 'log4j:log4j:1.2.16'

    testCompile 'junit:junit-dep:4.10'
    testCompile 'org.slf4j:slf4j-simple:1.6.1'
    testCompile 'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:2.22.1'
}

这个bean上的json序列化在我的常规json端点上运行得很好,但是jsonp似乎不起作用......它很简单,我甚至无法确定可能出错的地方。

0 个答案:

没有答案