title: Spring Rest Controller handling java.util.Optional using Jackson
body: <p>I have got Spring RestController classes to handle rest services using JSON. For JSON I am using Jackson. There are fields of java.util.Optional type</p>
  
  <pre><code> private Optional&lt;Long&gt; start = Optional.empty();
  </code></pre>
  
  <p>To enable the handling of Optional type, I am configuring the Spring as follows</p>
  
  <pre><code> &lt;bean id="objectMapper"  class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"&gt;
      &lt;property name="modulesToInstall" value="com.fasterxml.jackson.datatype.jdk8.Jdk8Module" /&gt; 
    &lt;/bean&gt;
  </code></pre>
  
  <p>However when I call the webservice, it fails in deserializing the Optional types with following message</p>
  
  <pre><code>org.springframework.http.converter.HttpMessageNotReadableException: 
     Could not read JSON: Can not instantiate value of type 
     [simple type, class java.util.Optional&lt;java.lang.Long&gt;] 
     from Long integral number (3424323423432); no 
     single-long-arg constructor/factory method
  </code></pre>
  
  <p>Doing serialization/deserialization from stand alone code works fine. There I register the module directly using the following code</p>
  
  <pre><code>    ObjectMapper m = new ObjectMapper();
      m.registerModule(new Jdk8Module());
  </code></pre>
  
  <p>Versions I am using:</p>
  
  <p>Spring : 4.1.5.RELEASE</p>
  
  <p>Jackson: 2.5.1</p>
  
  <p>Thanks in advance</p>