开发者

Best practises to convey parameters from JavaScript (jQuery) to a Java based REST service?

开发者 https://www.devze.com 2022-12-12 08:01 出处:网络
I have made a jQuery based widget which is configured a bit like this: jQuery(\"#foo\").widget(\"ser开发者_如何学Cvice\", {

I have made a jQuery based widget which is configured a bit like this:

jQuery("#foo").widget("ser开发者_如何学Cvice", {
    output : "test_output_field",
    parameters : {'format' : 'json',
                    'limit' : 20,
                    'services' : {'service1' : {},
                                'service2' : {'language' : 'en', 'type' : 'solid', 'parent' : 'father'},
                                'service3' : {'type' : 'big', 'strict' : 'true', 'filter' : 'all' }
                                }
    }
});

The parameters like 'format' and 'limit' are easy to handle; I just put them in the query string.

The services configuration part is the problem. Only thing that is constant is that there is an arbitrary number of services (here 'service1-3') with varying amount of parameters (specific to a certain service).

I have tried to configure them in a JSON string, but the parsers that are available for Java are horrible at best. I could parse the configuration to url parameters (like &service2_language=en), but the url could grow too long to handle.

What would you do?


the [JSON] parsers that are available for Java are horrible at best.

Somehow I find this hard to believe. Which parser were you using and what was the problem?


Long URLs with JSON data in them will indeed cause potential problems (eg length, escaping, etc), so you really should be using a POST method with the data in the body. I'd still say JSON is the best bet for serialization, because it is so natively supported in Javascript. Are you using jQuery or Prototype for doing the client side serialization?

YAML is another rather simple option. I use snakeyaml for Java YAML parsing, though there are occasional discrepancies in Yaml implementation as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消