java - 使用注释在剩余调用中传递 JSON 参数

标签 java javascript json rest backbone.js

这是我的 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){
        }
      }

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

最佳答案

字符串化的 json 不能在不编码的情况下作为查询参数直接在 URL 中发送。

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

关于java - 使用注释在剩余调用中传递 JSON 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29558681/

相关文章:

java - 禁用Tomcat中所有默认的HTTP错误响应内容

java - java中的比较器链

javascript - 将对象值作为单独的项目插入数组

python - 更新 json 文件

c# - 将 JValue 解析为 JObject 或 JArray

java - 如何在 Apache Flink 中正确初始化任务状态?

java - 尝试加载独立于平台的文件

javascript - Bootstrap 在加载时折叠隐藏导航链接

javascript - 无法检查元素是否有类,然后不为元素设置动画

java - 将 json 参数发布到 REST 服务时出错