Harshit
Harshit

Reputation: 1217

What is the maximum length of JSON object received by JAX-RS web service using GET?

I am trying to send JSON object as parameter.

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

During this process I am sending a big JSON object, similar to :

{"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}

my code works well until few number of parameters but not with as large object as mentioned above . Is there any limit to the length of JSONobject or other parameter to be sent over http request , If yes, then how to modify it.

Code:

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

   }

Upvotes: 0

Views: 5362

Answers (2)

eugen
eugen

Reputation: 5916

Yes there is a limit to urls length. It is however dependent on the browser and the web server (it is usually configurable, but I think it is a bad idea to change it...).

Usually if your url is less than 2000 characters it should always work. You can also have a look at this thread What is the maximum length of a URL in different browsers? And some other links maximum length of HTTP GET request?, http://www.boutell.com/newfaq/misc/urllength.html.

Anyway what you are doing is a bad practice. You should not pass json in the url, json should be used as the body of post requests. Having json as request parameter is not really REST style, there are discussions if url parameters is ok for REST apis, but it is discutable...

EDIT An example for jersey with post and using a library having databinding support:

Download genson library http://code.google.com/p/genson/, when it is in your classpath json databinding will be automatically enabled for jersey. Define classes corresponding to the json input (I called it ARequestBean) and AResponseBean containing the response that will be serialized to json.

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

Upvotes: 1

TheZuck
TheZuck

Reputation: 3621

GET requests used to be limited by IE browser to about 2k characters. It seems that browsers evolved a bit since then, and the limits grew with them.

In response to a question about GET request sizes (see here: Is there a limit to the length of a GET request?), someone responded the following:

I have done some more testing with IE8, IE9, FF14, Opera11, Chrome20 and Tomcat 6.0.32 (fresh installation), Jersey 1.13 on the server side. I used the jQuery function $.getJson and JSONP. Results: All Browsers allowed up to around 5400 chars. FF and IE9 did up to around 6200 chars. Everything above returned "400 Bad request". I did not further investigate what was responsible for the 400. I was fine with the maximum I found, because I needed around 2000 chars in my case. – OneWorld Jul 30 at 18:58

So the answer to your question is that it's not JSON limitation, it's GET limitation. If you POST your object instead of putting it on the URL you should be fine.

If your design forces you to put everything in the URL and it's a very big URL, you should reexamin your design, perhaps you should save some state on the server side and only pass an id.

Upvotes: 0

Related Questions