简体   繁体   English

将参数从JavaScript(jQuery)传递到基于Java的REST服务的最佳实践?

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

I have made a jQuery based widget which is configured a bit like this: 我制作了一个基于jQuery的小部件,其配置如下:

jQuery("#foo").widget("service", {
    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; 像'format'和'limit'这样的参数很容易处理。 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). 唯一不变的是,存在数量不定的服务(此处为“ service1-3”),且参数数量不同(特定于特定服务)。

I have tried to configure them in a JSON string, but the parsers that are available for Java are horrible at best. 我试图用JSON字符串配置它们,但是可用于Java的解析器充其量是可怕的。 I could parse the configuration to url parameters (like &service2_language=en), but the url could grow too long to handle. 我可以将配置解析为url参数(例如&service2_language = zh-CN),但是url可能会变得太长而无法处理。

What would you do? 你会怎么做?

the [JSON] parsers that are available for Java are horrible at best. 可用于Java的[JSON]解析器充其量是可怕的。

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. 其中包含JSON数据的长URL确实会引起潜在的问题(例如,长度,转义等),因此,您实际上应该对正文中的数据使用POST方法。 I'd still say JSON is the best bet for serialization, because it is so natively supported in Javascript. 我仍然要说JSON是序列化的最佳选择,因为Java原生支持JSON。 Are you using jQuery or Prototype for doing the client side serialization? 您是使用jQuery还是Prototype进行客户端序列化?

YAML is another rather simple option. YAML是另一个相当简单的选项。 I use snakeyaml for Java YAML parsing, though there are occasional discrepancies in Yaml implementation as well. 尽管Yaml实施中有时也会出现差异,但我使用snakeyaml进行Java YAML解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM