如何禁用spring boot参数拆分 [英] How to disable spring boot parameter split

查看:45
本文介绍了如何禁用spring boot参数拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有很多@RestController 接收用户用通用语言编写的短语.短语可以很长并且包含标点符号,例如句号,当然还有逗号.

We have many @RestController receiving phrases in common language written by users. Phrases can be very long and contains punctuation, like periods and, of course, commas.

简化的控制器示例:

@RequestMapping(value = "/countphrases", method = RequestMethod.PUT)
public String countPhrases(
    @RequestParam(value = "phrase", required = false) String[] phrase) {

  return "" + phrase.length;
}

Spring boot 的默认行为是以逗号分隔参数值,因此之前的控制器使用此 url 调用:

Spring boot default behaviour is to split parameters values at comma, so the previous controller called with this url:

[...]/countphrases?phrase=john%20and%20me,%20you%and%her

[...]/countphrases?phrase=john%20and%20me,%20you%and%her

将返回2"而不是我们想要的1".实际上用逗号分割前面的调用相当于:

Will return "2" istead of "1" like we want. In fact with the comma split the previous call is equivalent to:

[...]/countphrases?phrase=john%20and%20me&phrase=you%and%her

[...]/countphrases?phrase=john%20and%20me&phrase=you%and%her

我们使用自然语言,我们需要准确分析用户是如何写的,并准确知道他们写了多少.

We work with natural language and we need to analyze phrases exactly how the users wrote them and to know exactly how many they wrote.

我们尝试了这个解决方案:https://stackoverflow.com/a/42134833/1085716 在适应我们的弹簧启动版本(2.0.5):

We tried this solution: https://stackoverflow.com/a/42134833/1085716 after adapting it to our spring boot version (2.0.5):

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // we hoped this code could remove the "split strings at comma"
        registry.removeConvertible(String.class, Collection.class);
    }
}

但它不起作用.

有谁知道如何在 spring boot 2.0.5 中全局删除spring boot split string parameters at逗号"行为?

Anyone know how to globally remove the "spring boot split string parameters at comma" behaviour in spring boot 2.0.5?

推荐答案

我找到了解决方案.要覆盖默认转换,我们必须添加一个新转换.如果我们只删除旧的,那是行不通的.

I find the solution. To override a default conversion we must add a new one. If we remove the old one only it doesn't work.

正确的(示例)代码应该是:

The correct (example) code should be:

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.removeConvertible(String.class, String[].class);
        registry.addConverter(String.class, String[].class, noCommaSplitStringToArrayConverter());
    }

    @Bean
    public Converter<String, String[]> noCommaSplitStringToArrayConverter() {
        return new Converter<String, String[]>() {
            @Override
            public String[] convert(String source) {
                String[] arrayWithOneElement = {source};
                return arrayWithOneElement;
            }
        };
    }
}

这样,任何像主要问题中的控制器一样的控制器都不会拆分参数值:

This way any controller like the one in the main question will not split parameters values:

  • [...]/countphrases?phrase=a,b 将返回 1(和 fq=["a,b"])
  • [...]/countphrases?phrase=a,b&phrase=c,d 将返回 2(和 fq=["a,b", "c,d"])

这篇关于如何禁用spring boot参数拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆