title: spring mvc post convert Long to Date?
body: <p>Spring mvc controller</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST)
  public Result&lt;String&gt; create(CouponModel model) {//...}
  
  CuponModel:
  private Date beginDate;
  private Date overDate;
  </code></pre>
  
  <p>when call this api</p>
  
  <pre><code>curl -X POST -d '...&amp;beginDate=1459500407&amp;overDate=1459699200' 'http://localhost:8080/coupons'
  Error 400 Bad Request
  </code></pre>
  
  <p>first nothing error log output, then changed to <code>debug</code> level, ouput</p>
  
  <pre><code>Field error in object 'couponModel' on field 'beginDate': rejected value [1459500407]; codes [typeMismatch.couponModel.beginDate,typeMismatch.beginDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [couponModel.beginDate,beginDate]; arguments []; default message [beginDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'beginDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull java.util.Date for value '1459500407'; nested exception is java.lang.IllegalArgumentException]
  </code></pre>
  
  <p>How to let parameter long convert to date in model?</p>
  
  <p>I tried below way but it does not work</p>
  
  <pre><code>public class StringToDateConverter implements Converter&lt;String, Date&gt; {
  
      @Override
      public Date convert(String source) {
          if (source == null ) {
              return null;
          }
          return new Date(Long.valueOf(source));
      }
  
  }
  
  @Configuration
  @EnableWebMvc
  public class WebMvcContext extends WebMvcConfigurerAdapter {
      @Override
      public void addFormatters(FormatterRegistry registry) {
           registry.addConverter(new StringToDateConverter());
      }
  }
  </code></pre>