JAX-RS Web服务使用GET接收的JSON对象的最大长度是多少? [英] What is the maximum length of JSON object received by JAX-RS web service using GET?

查看:79
本文介绍了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}

我的代码可以很好地工作,直到很少数量的参数,但没有如上所述的大对象. 通过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";

   }

解决方案

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

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

无论如何,您的做法都是不好的做法.您不应该在url中传递json,而json应该用作发布请求的正文.将json作为请求参数并不是真正的REST风格,有人在讨论REST API的url参数是否可以使用,但可以讨论...

编辑 带有发布并使用具有数据绑定支持的库的jersey的示例:

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

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

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

   }

解决方案

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 ...;
}

这篇关于JAX-RS Web服务使用GET接收的JSON对象的最大长度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆