Axi online

  • Zwiększ rozmiar czcionki
  • Domyślny  rozmiar czcionki
  • Zmniejsz rozmiar czcionki
Start Java Adding custom Converters to ConversionService in Spring MVC

Adding custom Converters to ConversionService in Spring MVC

If somebody starts to play a little with a Sping MVC application sooner rather than later will see a message saying something like

blah, blah, blah, nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [sth.model.MyCustomType] for property 'propertyName': no matching editors or conversion strategy found

There are a lot of spnippets around how to create a converter, name it StringToMyCustomTypeConverter (I can recommend this one:, and also how to add it in the xml configuration:

<bean id="conversionService" class="">
  <property name="converters">
      <bean id="StringToMyCustomTypeConverter" class="sth.converter.StringToMyCustomTypeConverter" />

In my spring familiarization project I've decided to use java config to some extend. So I spent time looking around how to add custom converters in code. I easly run into problem with two ConversionService(s) registerd which ended with no explicit bean with name 'conversionService' error. Finally I found working (and very easy indeed) solution. My configuration class e.g. AppConfig (annotated with @Configuration), has to extend WebMvcConfigurerAdapter (or implement WebMvcConfigurer interface) and just override addFormatters method (you can register converters there also):

public void addFormatters(FormatterRegistry formatterRegistry) {
    formatterRegistry.addConverter(new StringToMyCustomTypeConverter());


Zaloguj się, aby móc pisać komentarze i odpowiedzi.



Naszą witrynę przegląda teraz 1113 gości 

Polecane strony


MTB Marathon


MTB Trophy