【问题标题】:Play framework get array of query string parameters from request.queryString播放框架从 request.queryString 获取查询字符串参数数组
【发布时间】:2017-04-10 16:02:23
【问题描述】:

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

【问题讨论】:

    标签: scala playframework get query-string


    【解决方案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

    【讨论】:

    • 感谢您的回答,但这是一个 scala 问题
    • 更新了答案,原理和实现基本都是两种语言;查看文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2011-09-08
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    相关资源
    最近更新 更多