使用注释在 rest 调用中传递 JSON 参数

Pass JSON parameter in rest call using annotations

这是我的Backbone模特

define([
    'underscore',
    'backbone',
    'models/baseModel'
], function (_, Backbone, BaseModel) {

    var category  = BaseModel.extend({
        initialize: function(options) {
            this.catId = options.catId;
        },
        url: function () {
            var test_json = {
                  k: {
                    k_catId: this.catId,
                    k_dateText: 'last30Days',
                    k_countryCode: $.cookie('countryCode'),
                  }
                }
            var json = JSON.stringify(test_json);
            return applicationUrl + "/service/user/brands/content-category/" + this.catId + "?dateText=last30Days&token=" + $.cookie('token') + "&countryCode=" + $.cookie('countryCode') + "&json=" + json
        }
    });
    return category;
});

我需要将此 json 传递到我的 java 后端,并希望通过“@QueryParam”属性 获取它。但它显示变量 "json" 不能作为解析器。我的java代码如下:

@POST
    @Path("/content-category/{catId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public final Object getContentCategoryData(@Auth AuthToken userAuth, @PathParam("catId") long catId, @QueryParam("dateText") String dateText,
                                               @QueryParam("countryCode") String countryCode, @QueryParam("json") String json) {
        long userId = userAuth.getUserId();
        HashMap<String, Object> response = new HashMap<String, Object>();
        try {
          //some code
          }
      catch(Exception e){
        }
      }

正在寻求任何类型的帮助。谢谢

您正在将参数作为 URL 的一部分发送,尝试修改 url 以指向端点并在 POST 中发送参数。我会用

model.set({
  yourData
});

然后model.save();

为什么后端没有 "token" 的参数?

@QueryParam("token") String token

尝试添加它。

字符串化的 json 不能在 url 中作为查询参数直接发送而不进行编码。

var json = encodeURI(JSON.stringify(json));