Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable spring boot parameter split

Tags:

spring-boot

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.

Simplified controller example:

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

  return "" + phrase.length;
}

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

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

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.

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);
    }
}

But it doesn't work.

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

like image 802
Znheb Avatar asked May 31 '26 23:05

Znheb


1 Answers

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 will return 1 (and fq=["a,b"])
  • [...]/countphrases?phrase=a,b&phrase=c,d will return 2 (and fq=["a,b", "c,d"])
like image 192
Znheb Avatar answered Jun 02 '26 20:06

Znheb