简体   繁体   English

Spring-boot 过滤器处理与查询参数

[英]Spring-boot filter handling with query params

In a REST endpoint, I want to receive filters as query parameters.在 REST 端点中,我想接收过滤器作为查询参数。 Each filter is defined in a key-value pair separated by commas as follows:每个过滤器都定义在一个用逗号分隔的键值对中,如下所示:

www.example.com/things?filter=key:value,key2:value2,key3:value3

This example means that the list of Things I'm trying to get must have the key as value , and the key2 as value2 and key3 as value3此示例意味着我要获取的事物列表必须具有key作为value key2作为value2key3作为value3

In this endpoint I can receive multiple filters, like this:在此端点中,我可以接收多个过滤器,如下所示:

www.example.com/things?filter=key:value&filter=key2:value2,key3:value3

This means that the list of things must have key as value or ( key2 as value2 and key3 as value3 )这意味着事物列表必须具有key作为valuekey2作为value2key3作为value3

In spring-boot, the way to receive multiple query parameters that have the same name is by defining in your controller a @RequestParam("filter") String[] filters .在 spring-boot 中,接收多个同名查询参数的方法是在 controller 中定义@RequestParam("filter") String[] filters But here's the issue: whenever I'll send just a single filter query parameter, I'll get an array of Strings formed by each key-pair value.但问题是:每当我只发送一个filter查询参数时,我都会得到一个由每个键对值组成的字符串数组。 If I send multiple filter , I'll get an array with each filter (as expected).如果我发送多个filter ,我将得到一个包含每个过滤器的数组(如预期的那样)。

This means that for the first example, I will have an array of size 3 for each key pair, while in the second example I would receive an array of Size 2.这意味着对于第一个示例,每个密钥对都有一个大小为 3 的数组,而在第二个示例中,我将收到一个大小为 2 的数组。

I need that, whenever I send just a single filter as query param, the @RequestParam tag delivers an array of size 1 with the whole String to be parsed later.我需要这样,每当我只发送一个filter作为查询参数时, @RequestParam标记都会提供一个大小为 1 的数组,其中包含稍后解析的整个字符串。 Is there a way to achieve this?有没有办法做到这一点?

There might be better ways to achieve what you're after, but this is my suggestion.可能有更好的方法来实现你所追求的,但这是我的建议。

A quick proof of concept, using Spring Boot 2.2.1.RELEASE , printing every filter on a separate line一个快速的概念证明,使用Spring Boot 2.2.1.RELEASE ,在单独的行上打印每个过滤器

import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/things")
public class ThingController {

    @GetMapping
    public void filters(@RequestParam MultiValueMap<String, String> filters) {
        System.out.println("Filters:");
        filters.get("filter").forEach(System.out::println);
    }
}

A curl command using only a single filter query param:仅使用单个filter查询参数的 curl 命令:

curl -v 'http://localhost:8080/things/?filter=key:value,key2:value2,key3:value3'

Prints:印刷:

Filters:
key:value,key2:value2,key3:value3

A curl command using only multiple filter query params:仅使用多个filter查询参数的 curl 命令:

curl -v 'http://localhost:8080/things/?filter=key:value&filter=key2:value2,key3:value3'

Prints:印刷:

Filters:
key:value
key2:value2,key3:value3

What you want to achieve is actually the default behaviour.您想要实现的实际上是默认行为。 If you declare an array in @RequestParam you will have the query parameters in the array no matter the number of parameters you send.如果您在 @RequestParam 中声明一个数组,无论您发送多少个参数,您都将在数组中拥有查询参数。

EDIT: For example, given this controller:编辑:例如,给定这个 controller:

@GetMapping("/things")
public ResponseEntity getThings(@RequestParam("filter") String[] filters) {

    return ResponseEntity.ok(filters);
}

You can call it in two different ways:您可以通过两种不同的方式调用它:

  • www.example.com/things?filter=key:value,key2:value2,key3:value3 www.example.com/things?filter=key:value,key2:value2,key3:value3
  • www.example.com/things?filter=key:value&filter=key2:value2&filter=key3:value3 www.example.com/things?filter=key:value&filter=key2:value2&filter=key3:value3

Both of them map to the same array:他们两个 map 到同一个阵列:

[
 "key:value",
 "key2:value2",
 "key3:value3"
]

EDIT2: I just checked that you can't mix the two ways. EDIT2:我刚刚检查过你不能混合这两种方式。 If you do so, the comma separated items will be in the same array item.如果这样做,逗号分隔的项目将在同一个数组项目中。 Maybe that was happening to you也许那发生在你身上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM