0

我正在尝试将 JSON 对象作为参数发送。

localhost:8080/HelloWorldApplication/webresources/helloworld/get/{param}

在这个过程中,我发送了一个大的 JSON 对象,类似于:

{"follow_request_sent": false, "profile_use_background_image": true, "contributors_enabled": false, "id": 200, "verified": false, "profile_image_url_https": "https://si0.twimg.com/sticky/default_profile_images/default_profile_4_normal.png", "profile_sidebar_fill_color": "e0ff92", "is_translator": false, "profile_text_color": "000000", "followers_count": 869, "profile_sidebar_border_color": "87bc44", "id_str": "200", "default_profile_image": true, "listed_count": 0, "status": {"favorited": false, "contributors": null, "truncated": false, "text": "http://goo.gl/OkfpC", "created_at": "2010-12-07T05:58:01", "retweeted": false, "in_reply_to_status_id": null, "coordinates": null, "source_url": "http://mobile.twitter.com", "source": "Mobile Web", "in_reply_to_status_id_str": null, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "id": 12023002585628672, "place": null, "retweet_count": 0, "geo": null, "in_reply_to_user_id_str": null, "possibly_sensitive": false, "id_str": "12023002585628672"}, "utc_offset": -28800, "statuses_count": 6, "description": "", "friends_count": 4, "location": "", "profile_link_color": "0000ff", "profile_image_url": "http://a0.twimg.com/sticky/default_profile_images/default_profile_4_normal.png", "notifications": false, "show_all_inline_media": false, "geo_enabled": true, "profile_background_color": "9ae4e8", "profile_background_image_url": "http://a0.twimg.com/images/themes/theme1/bg.png", "name": "Dalbir Singh", "lang": "en", "profile_background_tile": false, "favourites_count": 0, "screen_name": "dalbirsingh", "url": null, "created_at": "2006-04-29T01:00:27", "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme1/bg.png", "time_zone": "Pacific Time (US & Canada)", "protected": false, "default_profile": false, "following": false}

我的代码运行良好,直到参数数量很少,但没有像上面提到的那样大的对象。通过http请求发送的JSONobject或其他参数的长度是否有限制,如果有,那么如何修改它。

代码:

@GET 
    @Path("/get/{empno}")// this method process GET request from client
    @Produces("application/json")   // sends JSON
    public String getJson(@PathParam("empno") JSONObject p) {  // empno represents the empno sent from client   
        JSONObject obj = p;
        String x = obj.toString();
        System.out.println(x);
        //some function
        return "x";

   }
4

2 回答 2

1

是的,网址长度有限制。然而,它依赖于浏览器和 Web 服务器(它通常是可配置的,但我认为更改它是一个坏主意......)。

通常,如果您的网址少于 2000 个字符,它应该始终有效。你也可以看看这个线程什么是不同浏览器中 URL 的最大长度是多少? 以及其他一些链接最大长度的 HTTP GET 请求?, http://www.boutell.com/newfaq/misc/urllength.html

无论如何,您正在做的是一种不好的做法。你不应该在 url 中传递 json,json 应该用作 post 请求的主体。将 json 作为请求参数并不是真正的 REST 风格,如果 url 参数对于 REST apis 来说是可以的,有讨论,但它是可分割的......

编辑 带有帖子并使用具有数据绑定支持的库的球衣示例:

下载 genson 库http://code.google.com/p/genson/,当它在您的类路径中时,将为球衣自动启用 json 数据绑定。定义对应于 json 输入的类(我称之为 ARequestBean)和包含将被序列化为 json 的响应的 AResponseBean。

@POST 
@Path("/post")
@Consumes(("application/json") // consumes JSON
@Produces("application/json")   // sends JSON
public AResponseBean getJson(ARequestBean request) {
  return ...;
}
于 2012-10-25T11:30:40.430 回答
0

过去 IE 浏览器将 GET 请求限制为大约 2k 个字符。从那时起,浏览器似乎有了一些发展,限制也随之增加。

在回答有关 GET 请求大小的问题时(请参阅此处:是否有 GET 请求的长度限制?),有人回答如下:

我在服务器端对 IE8、IE9、FF14、Opera11、Chrome20 和 Tomcat 6.0.32(全新安装)、Jersey 1.13 进行了更多测试。我使用了 jQuery 函数 $.getJson 和 JSONP。结果:所有浏览器最多允许大约 5400 个字符。FF 和 IE9 最多处理了大约 6200 个字符。以上所有内容都返回“400 Bad request”。我没有进一步调查 400 的原因。我发现的最大值很好,因为我需要大约 2000 个字符。– 寰宇一家 7 月 30 日 18:58

所以你的问题的答案是它不是 JSON 限制,而是 GET 限制。如果您发布您的对象而不是将其放在 URL 上,您应该没问题。

如果您的设计迫使您将所有内容都放在 URL 中并且它是一个非常大的 URL,您应该重新检查您的设计,也许您应该在服务器端保存一些状态并且只传递一个 id。

于 2012-10-25T11:29:23.230 回答