0

I am running with Spring 3.2.18 (I know, it's old) with Spring MVC. My issue is that there is at least one default request parameter conversion (String -> List) that fails when there is actually only one item in the array, but it has commas. This is because the default conversion built into Spring will see it as a comma-separated list.

I am NOT using Spring Boot, so please avoid answers that specifically reference solutions using it.

I tried adding a @PostConstruct method to a @Confuguration class as follows:

@Configuration
public class MyConfig {
  @Autowired
  private ConfigurableEnvironment env;

  @PostConstruct
  public void removeConverters() {
    ConfigurableConversionService conversionService = env.getConversionService();
    conversionService.removeConvertible(String.class, Collection.class);
  }
}

This runs on startup but the broken conversion still occurs. I put a breakpoint in this method, and it is called only once on startup. I verified that it removed a converter that matched the signature of String -> Collection.

The following works, but when I put a breakpoint in the @InitBinder method it acts like it gets called once for every request parameter on every request.

@ControllerAdvice
public class MyController {
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    GenericConversionService conversionService = (GenericConversionService) binder.getConversionService();
    conversionService.removeConvertible(String.class, Collection.class);
  }
}

As I said the second one works, but it makes no sense that the offending converter has to be removed for every request made to that method - let alone for every request parameter that method takes.

Can someone please tell me why this only works when the removal is incredibly redundant? Better yet, please tell me how I'm doing the one-time, application-scope removal incorrectly.

  • I tried using the solution [here](https://stackoverflow.com/questions/25219061/java-config-equivalent-for-conversionservice-formattingconversionservicefactor). You have to add @EnableWebMvc to this class as well for it to actually run the `addFormatters` overridden method. Sadly, it seems like Spring still just discards the registered conversion service that is available at this point and completely rebuilds it using the defaults. – Jon Carmignani Feb 21 '23 at 01:11
  • When I set a breakpoint in `GenericConversionService` on the `converters` field definition, it looks like this is created and repopulated several (~5) times during web application startup. This seems excessive and ultimately unnecessary since it is only the last one that is used. – Jon Carmignani Feb 21 '23 at 01:20

0 Answers0