美文网首页
FeignClient pageable sort 无法desc

FeignClient pageable sort 无法desc

作者: 大继 | 来源:发表于2020-11-18 13:36 被阅读0次

    客户端:

    image.png

    GET http://product/products?search=status%3D%3DON%3Bcategories.id%3D%3Dff8080817495c0cf017495c28b0e0000&page=0&size=12&sort=price&sort=DESC

    服务端:

    image.png

    问题

    在url生成的排序,好像不大对

    生产了错误的查询
    sort=price&sort=DESC
    
    正确的为
    sort=price,DESC
    

    过程

    通过跟踪 PageableSpringEncoder 组成查询参数也没啥问题


    image.png

    sort=price,DESC 非常的正常,那就代表,在之后给弄坏了

    找到发生变化的地方
    ReflectiveFeign
    this.buildTemplateFromArgs.create(argv)

    问题

    QueryTemplate
    
    public String expand(Map<String, ?> variables) {
            String name = this.name.expand(variables);
            if (this.pure) {
                return name;
            } else {
                List<String> expanded = new ArrayList();
                Iterator var4 = this.values.iterator();
    
                while(var4.hasNext()) {
                    Template template = (Template)var4.next();
                    String result = template.expand(variables);
                    if (result != null) {
                        if (result.contains(",")) {
                            expanded.addAll(Arrays.asList(result.split(",")));
                        } else {
                            expanded.add(result);
                        }
                    }
                }
    
                return this.queryString(name, Collections.unmodifiableList(expanded));
            }
        }
    

    解决方法

    覆盖 QueryTemplate 类改写expand方法

    
    public String expand(Map<String, ?> variables) {
            String name = this.name.expand(variables);
            if (this.pure) {
                return name;
            } else {
                List<String> expanded = new ArrayList();
                Iterator var4 = this.values.iterator();
    
                while(var4.hasNext()) {
                    Template template = (Template)var4.next();
                    String result = template.expand(variables);
                    if (result != null) {
    //                    if (result.contains(",")) {
    //                        expanded.addAll(Arrays.asList(result.split(",")));
    //                    } else {
                            expanded.add(result);
    //                    }
                    }
                }
    
                return this.queryString(name, Collections.unmodifiableList(expanded));
            }
        }
    

    参考

    google到类型问题 的人
    https://www.gitmemory.com/issue/spring-cloud/spring-cloud-openfeign/146/533275272

    相关文章

      网友评论

          本文标题:FeignClient pageable sort 无法desc

          本文链接:https://www.haomeiwen.com/subject/vrkfiktx.html