使用REST发布项目列表 [英] POST a list of items using REST

查看:67
本文介绍了使用REST发布项目列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种约定,当我有一个(很长)要发送到服务器的项目列表时,如何序列化我的数据.

I'm looking for a convention on how to serialize my data when I have a (long) list of items that I want to POST to the server.

例如,如果我有一个资源/users,并且想要向其发布新资源,则可以对新用户的字段进行http-encode编码,然后将其放入请求正文中,如下所示:name=foo&age=20

For example, if I have a resource /users and I wanted to POST a new one to it, I'd http-encode the fields for the new user and put it in the request body like this: name=foo&age=20

但是,如果我有一个用户列表,例如[{ name: 'foo', age: 20 }, { name: 'bar', age: 10 }],是否有常规的发布方式?

But if I have a list of users, like this [{ name: 'foo', age: 20 }, { name: 'bar', age: 10 }], is there a conventional way of POSTing this?

我在考虑name[0]=foo&age[0]=20&name[1]=bar&age[1]=10,但找不到任何支持它的东西. Web服务器通常接受/期待什么?

I'm thinking name[0]=foo&age[0]=20&name[1]=bar&age[1]=10 but I can't find anything to back it up. What do web servers usually accept/expect?

推荐答案

可能会改变我答案的快速问题:您是直接从HTML表单发布信息,还是期望更复杂的内容(例如javascript处理,甚至不是Web)客户)

Quick question which may change my answer: Are you POSTing directly from an HTML form or are you expecting something more sophisticated (e.g. javascript processsing, or not even a web-based client)

如果您有足够复杂的客户端,则可以仅构造一个JSON字符串和内容类型为application/json的POST.然后,无论处理POST的任何资源,都可以使用任意数量的json库来读取发布的字符串并按原样进行处理.

If you have a sophisticated enough client, you could just construct a JSON string and POST with a content type of application/json. Then whatever resource is processing the POST could use any number of json libraries to read the posted string and process as is.

您使用什么框架/语言来构建REST服务?他们有内置的功能/约定可以为您提供帮助吗?

What framework/languages are you using to construct your REST service? Do they have built-in functionality/conventions to help you?

例如,如果您使用JAX-RS来构建服务,则有一个内置注释

For example if you're using JAX-RS to build your service, there is a built in annotation @FormParam which can be used to process posted forms... for example: if you posted the following with a content type of application/x-www-form-urlencoded: name=foo&age=20&name=bar&age=10

您可以通过以下方式在服务端检索并行列表:

You could retrieve parallel lists on the service side via:

@POST
@Consumes("application/x-www-form-urlencoded")
public void createUsers(@FormParam("name") List<String> name, @FormParam("age") List<String> age) {
    // Store your users
}

但是,您将不得不处理以下问题:如果一个列表比另一个列表短/长,该如何解决?如果需要一个新字段或一个可选字段来创建用户列表,会发生什么情况? (但是正如我最初提到的那样,JSON对象的JSON数组可以解决该问题……在JAX-RS中有许多库支持自动JSON反序列化,或者还可以选择创建自己的 MessageBodyReader .

But you would then have to deal with the question of what if one list is shorter/longer than the other, how do you resolve that? What happens if a new field is required or optional to create a list of users? (But as I mentioned initially, a JSON array of JSON objects would solve that issue... there are a number of libraries out there that support automagic JSON deserialization in JAX-RS or there is also the option of creating your own MessageBodyReader.

(下一节的免责声明:我不了解Rails,我的经验更多地是在Java服务领域...我将其基于

(Disclaimer on the next section: I don't know rails, my experience is more in the Java services world... I'm basing this on this guide). It looks like Rails has a convention of name[]=foo&name[]=bar to process posted data into arrays automagically, and a similar convention to populate structure like user[name]=foo&user[age]=20... Perhaps if you are on rails there is some way to use/abuse both of these features to get the desired result?

其他REST框架和语言可能具有自己的约定和功能:)

Other REST frameworks and languages may have their own conventions and functionality :)

这篇关于使用REST发布项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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