JAX-RS/Rest:多次设置参数,还是使用单个逗号分隔的参数?

发布于 2024-09-18 03:10:57 字数 550 浏览 2 评论 0原文

我读到在请求中传递数组的 HTTP 方式是多次设置参数:

1) GET /users?orderBy=last_name&orderBy=first_name

但是,我也看到了逗号分隔的参数(我觉得这是“更干净”):

2) GET /users?orderBy=last_name,first_name

我想实现多个排序(按姓氏对用户进行排序,然后按名字对重复的姓氏进行排序)。从代码角度来看,这很容易(Google 的 Guava 库可以拯救),但我应该如何公开它?第一种方法是否保留字段的顺序(按姓氏排序,然后按名字排序)?

如果在请求中多次设置参数,Spring 会神奇地将参数转换为 String[] 数组:

... @RequestParam("orderBy") String[] orderBy ... becomes ["last_name","first_name"]

这使我相信第一种方法被认为是最佳实践,尽管我喜欢第二种方法......

I read that the HTTP way to pass an array in a request is to set a parameter multiple times:

1) GET /users?orderBy=last_name&orderBy=first_name

However, I've also seen the comma-delimited parameter (and I feel this is "cleaner"):

2) GET /users?orderBy=last_name,first_name

I want to implement multi-sorting (ordering users by last_name, then duplicate last_names are ordered by first_name). Code-wise, this is easy (Google's Guava libraries to the rescue), but how should I expose this? Does the first way even preserve the order of the fields (sort by last_name, then by first_name)?

Spring will magically convert a parameter into a String[] array, if it is set multiple times in the request:

... @RequestParam("orderBy") String[] orderBy ... becomes ["last_name","first_name"]

This leads me to believe the first way is considered best-practice, although I like the second way...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-09-25 03:11:14

我认为这是一个见仁见智的问题。 JAX-RS 允许您拥有如下参数:

@QueryParam("orderBy") List<String> orderBy

我认为这与 Spring 关于“神奇转换”部分的作用相同。我不一定认为这表明是否是“最佳实践”。只是某些参数可以有多个值,并且框架允许您读取这些多个值(想想某些 HTML 表单)。

就我个人而言,我会使用以逗号分隔的单个值。正如您所说,它“更干净”,并且该值更容易构建(您不依赖于参数键/值的顺序,这可能会给客户端开发人员带来一些麻烦)。

I think it's a matter of opinion. JAX-RS allows you to have parameters like:

@QueryParam("orderBy") List<String> orderBy

which I think would do the same thing as Spring regarding "magically convert" part. I don't necessarily think this is indicative of a "best practice" or not. It's just that some parameters can have multiple values and the frameworks allow you to read those multiple values (think certain HTML forms).

Personally, I would use a single value that's comma delimited. It's "cleaner" like you said and the value is easier to build (you're not relying on the order of parameter key/values which can lead to some trouble for client developers).

骑趴 2024-09-25 03:11:11

第一种方法是首选的标准方法。

您当然可以使用第二种方式,但是您必须实现自己的方式来标记请求参数值,并解决其中涉及的所有问题。例如,考虑一下如果您的值之一包含“,”字符会发生什么情况。

因为第一个是相当标准的,所以它的优点是可以很好地适应 jax-rs 和验证框架;因为我们总是验证我们的输入,对吗? ;)

The first method is the preferred, standard way.

You can certainly use the second way, but you will have to implement your own way of tokenizing the request parameter value, with all the issues that involves. For example, consider what happens if one of your values contains a ',' character.

Because the first is quite standard, it has the benefit of fitting in nicely with jax-rs, and validation frameworks; because we always validate our inputs, right? ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文