快速发送“地图”作为查询参数

时间:2018-10-16 15:52:09

标签: swift spring

这是一个简单的问题,但我不知道如何执行此操作。我有一个如下所示的Java后端:

@RequestMapping(value = "/batch", method = RequestMethod.GET)
public List<EventElasticsearchDTO> getBatch(
        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
        @RequestParam(value = "paginationToken", required = false, defaultValue = "0") Integer paginationToken,
        @RequestParam(value = "lat") Double latitude,
        @RequestParam(value = "long") Double longitude,
        @RequestParam Map<String, String> queryOptions) throws IOException {...}

我的问题是,在Swift中,如何发送 queryOptions 参数。否则,如果我的网址看起来像这样:https://api.example.net/api/event/batch?paginationToken=5&long=-165.0312186&lat=45.33233141,那么在添加queryOptions时应该显示什么?

已在使用URLQueryComponents。我想知道如何使用URLQueryComponents发送地图。

if let queryParams = queryParams {
  components.queryItems = queryParams.map { (key, value) -> URLQueryItem in
    URLQueryItem(name: key, value: value)
  }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

Map<String, String> queryOptions用于将所有参数 s 保留为Map。您只需要发送一个额外的参数,服务器端会将它的名称放入queryOptions

//`queryParams` may be a parameter to some method, here just to show how to do it...
let queryParams: [String: String]? = [
    "paginationToken": "5",
    "long": "-165.0312186",
    "lat": "45.33233141",
    "maxItems": "1000000", //<- Just add it
]

if let queryParams = queryParams {
    components.queryItems = queryParams.map { (key, value) -> URLQueryItem in
        URLQueryItem(name: key, value: value)
    }
}

在您的服务器端,除命名参数外的所有其他参数都放入queryOptions中。