Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对于 op=mv / bt 的查询,使用 ParamFilter 来简化多值传参,例如:用 age=[20,30] 替代 age-0=20&age-1=30 #10

Open
troyzhxu opened this issue Nov 23, 2021 · 3 comments
Labels

Comments

@troyzhxu
Copy link
Owner

No description provided.

@troyzhxu troyzhxu changed the title 待优化:对于 op=mv / bt 的查询,可用 JSON 数组来传输多个值,例如:age=[20,30] 替代 age-0=20&age-1=30 对于 op=mv / bt 的查询,如何用 JSON 数组来传输多个值,例如:age=[20,30] 替代 age-0=20&age-1=30 Nov 26, 2021
@troyzhxu
Copy link
Owner Author

troyzhxu commented Nov 26, 2021

在 Spring Boot 框架下,且使用的是 bean-searcher-boot-starter 依赖,则只需注册一个 Bean 即可实现:

@Bean
public ParamFilter myParamFilter() {
    return new ParamFilter() {

        final String OP_SUFFIX = "-op";

        @Override
        public <T> Map<String, Object> doFilter(BeanMeta<T> beanMeta, Map<String, Object> paraMap) {
            Map<String, Object> newParaMap = new HashMap<>();
            paraMap.forEach((key, value) -> {
                if (key == null) {
                    return;
                }
                boolean isOpKey = key.endsWith(OP_SUFFIX);
                String opKey = isOpKey ? key : key + OP_SUFFIX;
                Object opVal = paraMap.get(opKey);
                if (!"mv".equals(opVal) && !"bt".equals(opVal)) {
                    newParaMap.put(key, value);
                    return;
                }
                if (newParaMap.containsKey(key)) {
                    return;
                }
                String valKey = key;
                Object valVal = value;
                if (isOpKey) {
                    valKey = key.substring(0, key.length() - OP_SUFFIX.length());
                    valVal = paraMap.get(valKey);
                }
                if (likelyJsonArr(valVal)) {
                    try {
                        String vKey = valKey;
                        JSONKit.toArray((String) valVal).forEach(
                            (index, data) -> newParaMap.put(vKey + "-" + index, data.toString())
                        );
                        newParaMap.put(opKey, opVal);
                        return;
                    } catch (Exception ignore) {}
                }
                newParaMap.put(key, value);
            });
            return newParaMap;
        }

        private boolean likelyJsonArr(Object value) {
            if (value instanceof String) {
                String str = ((String) value).trim();
                return str.startsWith("[") && str.endsWith("]");
            }
            return false;
        }

    };
}

@troyzhxu troyzhxu changed the title 对于 op=mv / bt 的查询,如何用 JSON 数组来传输多个值,例如:age=[20,30] 替代 age-0=20&age-1=30 对于 op=mv / bt 的查询,使用 ParamFilter 实现用 JSON 数组来简化多值传参,例如:age=[20,30] 替代 age-0=20&age-1=30 Nov 26, 2021
@troyzhxu troyzhxu changed the title 对于 op=mv / bt 的查询,使用 ParamFilter 实现用 JSON 数组来简化多值传参,例如:age=[20,30] 替代 age-0=20&age-1=30 对于 op=mv / bt 的查询,使用 ParamFilter 实现:用 JSON 数组来简化多值传参,例如:age=[20,30] 替代 age-0=20&age-1=30 Nov 26, 2021
@troyzhxu
Copy link
Owner Author

troyzhxu commented Nov 26, 2021

上述代码中用到的 JSON 解析工具 JSONKit,它是一个 超级轻量的 JSON 门面工具,以下依赖 只需添加一个 即可:

Gradle

implementation 'com.ejlchina:jsonkit-jackson:1.3.3'      // 使用 Jackson 底层实现
implementation 'com.ejlchina:jsonkit-gson:1.3.3'          // 使用 Gson 底层实现
implementation 'com.ejlchina:jsonkit-fastjson:1.3.3'      // 使用 Fastjson 底层实现

Maven

<!--  使用 Jackson 底层实现 -->
<dependency>
    <groupId>com.ejlchina</groupId>
    <artifactId>jsonkit-jackson</artifactId>
    <version>1.3.3</version>
</dependency>
<!--  使用 Gson 底层实现 -->
<dependency>
    <groupId>com.ejlchina</groupId>
    <artifactId>jsonkit-gson</artifactId>
    <version>1.3.3</version>
</dependency>
<!--  使用 Fastjson 底层实现 -->
<dependency>
    <groupId>com.ejlchina</groupId>
    <artifactId>jsonkit-fastjson</artifactId>
    <version>1.3.3</version>
</dependency>

当然上述参数过滤器的实现, 使用其它 JSON 工具也是可以的。

@troyzhxu troyzhxu changed the title 对于 op=mv / bt 的查询,使用 ParamFilter 实现:用 JSON 数组来简化多值传参,例如:age=[20,30] 替代 age-0=20&age-1=30 对于 op=mv / bt 的查询,使用 ParamFilter 来简化多值传参,例如:用 age=[20,30] 替代原来的 age-0=20&age-1=30 Nov 26, 2021
@troyzhxu troyzhxu changed the title 对于 op=mv / bt 的查询,使用 ParamFilter 来简化多值传参,例如:用 age=[20,30] 替代原来的 age-0=20&age-1=30 对于 op=mv / bt 的查询,使用 ParamFilter 来简化多值传参,例如:用 age=[20,30] 替代 age-0=20&age-1=30 Nov 26, 2021
@troyzhxu
Copy link
Owner Author

troyzhxu commented Nov 26, 2021

需要注意

当这样直接请求时:/user/index?age=[20,30]&age-op=bt
服务器可能会报错:Invalid character found in the request target [/user/index?age=[20,30]&age-op=bt ]. The valid characters are defined in RFC 7230 and RFC 3986

需要对 [20,30] 进行一次 URLEncode,这样就 OK 了:
/user/index?age=%5B20,30%5D&age-op=bt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant