简体   繁体   English

@RequestParam数组映射问题

[英]@RequestParam array mapping issues

I'm doing a REST service with Spring MVC framework. 我正在使用Spring MVC框架进行REST服务。

I have a method: 我有一个方法:

@RequestMapping("/rest/{tableName}", method = RequestMethod.GET)
public @ResponseBody CustomObject query(
    @PathVariable("tableName") String tableName, 
    @RequestParam(value="columns", required=false) String[] columns,
    @RequestParam(value="filter", required=false) String[][] filters) {
...
}

Well, I want filters to be a bidimensional array which an specific structure: 好吧,我希望过滤器成为具有特定结构的二维数组:

If I do a request with this url /rest/table?filter=filter1,filter2&filter=filter3 I have then filters = {{filter1, filter2}, {filter3}} 如果我使用此网址/rest/table?filter=filter1,filter2&filter=filter3发出请求,请执行以下操作: /rest/table?filter=filter1,filter2&filter=filter3然后,我的filters = {{filter1, filter2}, {filter3}}

If I do with: /rest/table?filter=filter1&filter=filter2 I have: filters = {{filter1},{filter2}} 如果我这样做: /rest/table?filter=filter1&filter=filter2我有: filters = {{filter1},{filter2}}

My question is... 我的问题是

why if I call: /rest/table?filter=filter1,filter2 I get: filters = {{filter1},{filter2}} and not: filters = {{filter1,filter2}} ? 为什么调用: /rest/table?filter=filter1,filter2我得到: filters = {{filter1},{filter2}}而不是: filters = {{filter1,filter2}}

Is there any way for get the last array instead of the first in that situation? 在这种情况下,有什么方法可以获取最后一个数组而不是第一个数组?

Sending lists of items in the URL is tricky. 在URL中发送项目列表非常棘手。 In general, the request 一般来说,要求

/rest/table?filter=A&filter=B

and

/rest/table?filter=A,B

will both get parsed as if A and B are individual parameters. 都将被解析,就好像A和B是单独的参数一样。 This is because Spring's default WebDataBinder is configured to split parameters lists on commas. 这是因为将Spring的默认WebDataBinder配置为在逗号上拆分参数列表。 You can disable this default configuration by adding some binder initialization code to your controller. 您可以通过向控制器添加一些绑定程序初始化代码来禁用此默认配置。

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(
        String[].class,
        new StringArrayPropertyEditor(null)); 
}

Now the data binding process for parameter lists coming in over HTTP will not be split on the comma, and be interpreted as separate items. 现在,通过HTTP传入的参数列表的数据绑定过程将不会在逗号上拆分, 而是被解释为单独的项。 This will likely produce the behavior you're looking for, so that comma-delimited parameter lists will be treated as a single array parameter rather than N separate single-item array parameters. 这很可能会产生您要寻找的行为,因此以逗号分隔的参数列表将被视为单个数组参数,而不是N个单独的单项数组参数。

Instead of only disabling the usage of comma as separator, for overriding the use of a comma delimiter with pipe I did the following: 为了替代使用逗号分隔符与管道,我不仅执行了以下操作:

1 - Created a class that extends PropertyEditorSupport setting the delimiter as pipe. 1-创建了一个扩展PropertyEditorSupport的类,将分隔符设置为管道。

public class StringListPropertyEditor extends PropertyEditorSupport {

    public static final String DEFAULT_SEPARATOR = "\\|";

    @Override
    public void setAsText(String text) throws IllegalArgumentException {

        List<String> theList = Arrays.asList(text.split(DEFAULT_SEPARATOR));

        setValue(theList);

    }


}

2 - Created a InitBinder at my controller which register my PropertyEditor as CustomEditor 2-在我的控制器上创建了一个InitBinder,将我的PropertyEditor注册为CustomEditor

@InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(List.class, new StringListPropertyEditor());
    }

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

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