title: REST response object with Java Optional getters
body: <p>I am writing a REST Service in Spring Boot. I want to make some of the fields of my DTO classes to be accessed as Optional getters. Here is the example.</p>
  
  <pre><code>@JsonInclude(Include.NON_NULL)
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "profileType", visible = true, defaultImpl = InvalidUserDTO.class)
  @JsonSubTypes({ @JsonSubTypes.Type(value = ClientDTO.class, name = "CLIENT"), @JsonSubTypes.Type(value = DriverDTO.class, name = "DRIVER") })
  public abstract class UserDTO {
  
      private String password;
  
      private Optional&lt;String&gt; email;
  </code></pre>
  
  <p>With such approach, in my REST response I am getting something like <code>email: {"present" : true}</code> instead of the real value of the email.</p>
  
  <p>To resolve this, I tried adding the Jdk8Module dependency to the classpath.</p>
  
  <p>However, that only caused the following error while trying to call my service on the endpoints that use Optionals:</p>
  
  <pre><code>    {"message":"Internal Server Error","details":"Handler dispatch failed; nested exception is java.lang.AbstractMethodError: 
  
  com.fasterxml.jackson.databind.ser.std.ReferenceTypeSerializer.withResolved(Lcom/fasterxml/jackson/databind/BeanProperty;Lcom/fasterxml/jackson/databind/jsontype/TypeSerializer;
  Lcom/fasterxml/jackson/databind/JsonSerializer;Lcom/fasterxml/jackson/databind/util/NameTransformer;Lcom/fasterxml/jackson/annotation/JsonInclude$Include;)
  Lcom/fasterxml/jackson/databind/ser/std/ReferenceTypeSerializer;"}
  </code></pre>
  
  <p>Is there something I am missing? Why does that happen?</p>
  
  <p>Here is the ObjectMapper config:</p>
  
  <pre><code>@SpringBootApplication
  public class MainApplication extends SpringBootServletInitializer {
  
      @Autowired
      void configureObjectMapper(final ObjectMapper mapper) {
          mapper
              .registerModule(new ParameterNamesModule())
              .registerModule(new Jdk8Module());
      }    
  
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
          servletContext.setInitParameter("webAppRootKey", "newfacesBackend.root");
          servletContext.setInitParameter("isLog4jAutoInitializationDisabled", "true");
          super.onStartup(servletContext);
      }
  
      public static void main(String[] args) throws Exception {
          SpringApplication.run(MainApplication.class, args);
      }
  
  }
  </code></pre>
  
  <p>Gradle dependencies:</p>
  
  <pre><code>buildscript {
      dependencies {
          classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
      }
  }
  
  apply plugin: 'com.github.ben-manes.versions'
  apply plugin: 'maven-publish'
  apply plugin: 'org.springframework.boot'
  
  
  configurations {
      compile.exclude group:'ch.qos.logback'
  }
  
  dependencies {
  
  
      compile("org.springframework.boot:spring-boot-starter-actuator")
      compile("org.springframework.boot:spring-boot-starter-web")
      compile("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.9.4")
  
      compile("com.fasterxml.jackson.module:jackson-module-parameter-names:2.9.4")
  </code></pre>