Play框架从request.queryString获取查询字符串参数数组

时间:2017-04-10 16:02:24

标签: scala playframework get query-string

routes.conf

GET /api/v1/jurisdictions controllers.v1.JurisdictionController.getJurisdictions()

JurisdictionController

  def getJurisdictions() = Action { implicit request =>
      // this is returning None
      val filters = request.queryString.get("filters") 


      val result = jurisdictionService.getJurisdictions()
      Ok(serializer.serialize(result)).as("application/json")
  }

相关请求URI:

http://localhost:9000/api/v1/jurisdictions?filter[name]=Ryan&filter[number]=333333

如何获取此查询字符串filter

1 个答案:

答案 0 :(得分:-1)

您必须创建一个自定义绑定器,这是一个Java实现,但它遵循相同的原则:

public class AgeRange implements QueryStringBindable<AgeRange> {
    public Integer from;
    public Integer to;

    //A simple example of the binder’s use binding the :from and :to query string parameters:

    @Override
    public Optional<AgeRange> bind(String key, Map<String, String[]> data) {

        try{
            from = new Integer(data.get("from")[0]);
            to = new Integer(data.get("to")[0]);
            return Optional.of(this);

        } catch (Exception e){ // no parameter match return None
            return Optional.empty();
        }
    }

    @Override
    public String unbind(String key) {
        return new StringBuilder()
            .append("from=")
            .append(from)
            .append("&to=")
            .append(to)
            .toString();
    }
}

Java documentation

Scala documentation