这是我的 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/