如何在URL请求中发送数组 [英] how to send an array in url request

查看:386
本文介绍了如何在URL请求中发送数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求如下:

我想给演员一个名字,开始日期,结束日期,并获取他在那段时间里演过的所有电影.

I want to give actor name, start date, end date and get all the films he acted in that period.

因此,我的服务请求就是这样.

For that reason, my service request is like this.

  http://localhost:8080/MovieDB/GetJson?name=Actor&startDate=20120101&endDate=20120505

现在,我想改善它. 我想提供开始日期,结束日期和一个以上的演员姓名,并希望查看该时期内所有这些演员的电影.

Now, i want to improve it. I want to give a start date, end date and more than one actor name and want to see all those actors movies in that period.

我不确定我的网址应该如何支持这种情况.

I am not sure how should my url look to support such thing.

我正在使用spring编写基于Java的Web服务.

I am writing a java based web service using spring.

下面的代码是为了支持一个演员

Below code is to support one actor

   @RequestMapping(value = "/GetJson", method = RequestMethod.GET) 
    public void getJson(@RequestParam("name") String ticker, @RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
   //code to get results from db for those params.
 }

我正在考虑的一种解决方案是使用%符号分隔演员名称.例如:

One solution i am thinking is using a % symbol to seperate actor names. For example:

 http://localhost:8080/MovieDB/GetJson?name=Actor1%Actor2%Actor3&startDate=20120101&endDate=20120505

现在,在控制器中,我将使用%解析名称字符串,并获取所有演员名称.

Now, in the controller i will parse the name string with % and get back all actors names.

这是执行此操作的好方法还是标准方法?

Is this a good way to do this or is there a standard approach?

谢谢

推荐答案

以逗号分隔:

http://localhost:8080/MovieDB/GetJson?name=Actor1,Actor2,Actor3&startDate=20120101&endDate=20120505

或:

http://localhost:8080/MovieDB/GetJson?name=Actor1&name=Actor2&name=Actor3&startDate=20120101&endDate=20120505

或:

http://localhost:8080/MovieDB/GetJson?name[0]=Actor1&name[1]=Actor2&name[2]=Actor3&startDate=20120101&endDate=20120505

无论哪种方式,您的方法签名都必须是:

Either way, your method signature needs to be:

@RequestMapping(value = "/GetJson", method = RequestMethod.GET) 
public void getJson(@RequestParam("name") String[] ticker, @RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate) {
   //code to get results from db for those params.
 }

这篇关于如何在URL请求中发送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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