title: Spring MVC type conversion : PropertyEditor or Converter?
body: <p>I am looking for the easiest and simplest way to bind and convert data in Spring MVC. If possible, without doing any xml configuration. </p>
  
  <p>So far I've been using <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#beans-beans-conversion-customeditor-registration" rel="noreferrer">PropertyEditors</a> like so :</p>
  
  <pre><code>public class CategoryEditor extends PropertyEditorSupport {
  
      // Converts a String to a Category (when submitting form)
      @Override
      public void setAsText(String text) {
          Category c = new Category(text);
          this.setValue(c);
      }
  
      // Converts a Category to a String (when displaying form)
      @Override
      public String getAsText() {
          Category c = (Category) this.getValue();
          return c.getName();
      }
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>...
  public class MyController {
  
      @InitBinder
      public void initBinder(WebDataBinder binder) {
          binder.registerCustomEditor(Category.class, new CategoryEditor());
      }
  
      ...
  
  }
  </code></pre>
  
  <p>It is simple : both conversion are defined in the same class, and the binding is straightforward. If I wanted to do a general binding across all my controllers, I could still add <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-webdatabinder" rel="noreferrer">3 lines in my xml config</a>.</p>
  
  <hr>
  
  <p>But Spring 3.x introduced a new way to do it, using <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#core-convert" rel="noreferrer">Converters</a> :</p>
  
  <blockquote>
    <p>Within a Spring container, this system can be used as an alternative
    to PropertyEditors</p>
  </blockquote>
  
  <p>So let's say I want to use Converters because it is "the latest alternative". I would have to create <strong>two</strong> converters :</p>
  
  <pre><code>public class StringToCategory implements Converter&lt;String, Category&gt; {
  
      @Override
      public Category convert(String source) {
          Category c = new Category(source);
          return c;
      }
  
  }
  
  public class CategoryToString implements Converter&lt;Category, String&gt; {
  
      @Override
      public String convert(Category source) {
          return source.getName();
      }
  
  }
  </code></pre>
  
  <p><strong>First drawback :</strong> I have to make two classes. Benefit : no need to cast thanks to genericity.</p>
  
  <p>Then, how do I simply data bind the converters ?  </p>
  
  <p><strong>Second drawback :</strong> I haven't found any simple way (annotations or other programmatic facilities) to do it in a controller : nothing like <code>someSpringObject.registerCustomConverter(...);</code>.</p>
  
  <p>The only ways I've found would be tedious, not simple, and only about general cross-controller binding :</p>
  
  <ul>
  <li><p><a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#core-convert-Spring-config" rel="noreferrer">XML config</a> :</p>
  
  <pre><code>&lt;bean id="conversionService"
    class="org.springframework.context.support.ConversionServiceFactoryBean"&gt;
      &lt;property name="converters"&gt;
          &lt;set&gt;
              &lt;bean class="somepackage.StringToCategory"/&gt;
              &lt;bean class="somepackage.CategoryToString"/&gt;
          &lt;/set&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre></li>
  <li><p><a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-customize" rel="noreferrer">Java config</a> (<strong>only in Spring 3.1+</strong>) :</p>
  
  <pre><code>@EnableWebMvc
  @Configuration
  public class WebConfig extends WebMvcConfigurerAdapter {
  
      @Override
      protected void addFormatters(FormatterRegistry registry) {
          registry.addConverter(new StringToCategory());
          registry.addConverter(new CategoryToString());
      }
  
  }
  </code></pre></li>
  </ul>
  
  <p>With all these drawbacks, why using Converters ? Am I missing something ? Are there other tricks that I am not aware of ?</p>
  
  <p>I am tempted to go on using PropertyEditors... Binding is much easier and quicker.</p>