JAX-RS Web服务使用GET收到的JSON对象的最大长度是多少



我试图将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}

我的代码效果很好,直到几个参数数量,但没有如上所述的大对象。是否有任何限制JSONOBJECT或其他参数要通过HTTP请求发送的限制,如果是,则如何修改。

代码:

@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";
   }

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

通常,如果您的URL小于2000个字符,则应该始终工作。您还可以查看此线程在不同的浏览器中的URL的最大长度是多少?还有其他一些链接http获取请求的最大长度?

无论如何,您正在做的是一种不好的做法。您不应在URL中传递JSON,应将JSON用作邮政请求的主体。将JSON作为请求参数并不是真正的休息样式,如果REST API可以使用URL参数,则进行讨论,但它是可行的...

编辑邮政邮政和使用具有数据指标支持的库的示例:

下载Genson Library http://code.google.com/p/genson/,当它在您的类Path JSON数据贴上时,将自动启用泽西岛。定义与JSON输入相对应的类(我称其为arequestBean)和Aresponsebean,其中包含将序列化的响应。

@POST 
@Path("/post")
@Consumes(("application/json") // consumes JSON
@Produces("application/json")   // sends JSON
public AResponseBean getJson(ARequestBean request) {
  return ...;
}

获得曾经的浏览器限制为2k字符的请求。从那时起,浏览器似乎有所发展,并且限制随着它们而增长。

回答有关获取请求大小的问题(请参阅此处:get请求的长度有限制吗?),有人回复了以下内容:

我已经对IE8,IE9,FF14,Opera11,Chrome20和Tomcat 6.0.32(新安装)进行了更多测试,服务器端的泽西岛1.13。我使用了jQuery函数$ .getjson和JSONP。结果:所有浏览器都允许大约5400个字符。FF和IE9大约有6200个字符。上面的所有内容都返回了" 400不良请求"。我没有进一步调查造成400的原因。我发现的最大值很好,因为在我的情况下我需要2000个字符。 - Oneworld 30年7月18:58

因此,您的问题的答案是它不是JSON的限制,而是受到限制。如果您发布对象而不是将其放在URL上,则应该可以。

如果您的设计迫使您将所有内容都放在URL中,并且它是一个很大的URL,则应重新检查设计,也许您应该在服务器端保存某些状态并仅通过ID。

最新更新