There is a significant inconsistency when converting a query parameter from a collection to a single value. In the following example controller, the value received by the method varies unpredictably when multiple values are provided, leading to inconsistent behavior.

@RestController
public class SampleController {

    @PostMapping("/myIntegerParam")
    public String printIntegerParameter(@RequestParam(required = false) Integer param) {
        System.out.println("Query parameter: " + param);
        return "Received param: " + param;
    }

    @PostMapping("/myStringParam")
    public String printStringParameter(@RequestParam(required = false) String param) {
        System.out.println("Query parameter: " + param);
        return "Received param: " + param;
    }
}

If you send a /myIntegerParam?param=1&param=2 request, the printIntegerParameter() method will only receive the first value 1. But if you send a /myStringParam?param=1&param=2 request, the printStringParameter() method will receive both values comma separated as a single string: 1,2

The reason why this is happening is because in the 1st case the conversion is handled by the org.springframework.core.convert.support.ArrayToObjectConverter class, while in the 2nd by the org.springframework.core.convert.support.ArrayToStringConverter class.

I'm not sure whether this is a deliberate design decision or simply Spring delegating the conversion in its standard way. However, for a query parameter, this behavior introduces several issues:

  1. Inconsistency – This approach is inconsistent with how other types are handled (e.g., integers in my example). For most types, Spring keeps only the first value, but for strings, it concatenates them. Why should a developer expect this discrepancy?

  2. Lack of Practical Use – The resulting value is not meaningful. It is neither the first nor the last provided value, nor any actual value passed. This makes it unclear how a developer should handle it.

  3. Incorrect Assumption – If a developer intended to get multiple values, they would have used a list rather than a single string.

  4. Unnecessary Complexity – Given that string query parameters are extremely common, this unusual behavior forces developers to add extra validations and custom converters to ensure expected behavior.

To maintain consistency, the best approach when converting multiple query parameters into a single value is to always return the same value (either the first or last)

The above behavior was observed with: - 'org.springframework.boot' version '3.4.3' - 'org.springframework:spring-core' version '6.2.3'