title: RESTful service in Spring configuration with jackson-datatype-jsr310
body: <p>I am trying to configure a Java 8 &amp; Spring 4.3.1 app to use RESTful services.  I get it to work perfectly with the config below, until I introduce a <strong>ContextResolver</strong>. </p>
  
  <p>The reason for the ContextResolver is because I need to format a <code>java.time.LocalDateTime</code> to <strong>JSON</strong>. </p>
  
  <p>I first tried with <strong>annotations</strong> on my model bean by adding the <strong>@JsonFormat</strong>,</p>
  
  <pre><code>@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_FORMAT)    
  @DateTimeFormat(pattern=DATE_FORMAT) 
  @Column(name = "JOINING_DATE", nullable = false)
  @Type(type="org.hibernate.type.LocalDateTimeType")
  private LocalDateTime joiningDate;
  </code></pre>
  
  <p>and got the following <strong>error</strong>:</p>
  
  <blockquote>
    <p>java.lang.NoSuchMethodError:
    com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase.findFormatOverrides(Lcom/fasterxml/jackson/databind/SerializerProvider;Lcom/fasterxml/jackson/databind/BeanProperty;Ljava/lang/Class;)Lcom/fasterxml/jackson/annotation/JsonFormat$Value;</p>
  </blockquote>
  
  <p>Secondly, I removed the @JsonFormat annotation and tried with a <strong>ContextResolver</strong>, </p>
  
  <p><strong>ObjectMapperContextResolver.java</strong></p>
  
  <pre><code>package com.jobs.spring.configuration;
  
  import javax.ws.rs.ext.ContextResolver;
  import javax.ws.rs.ext.Provider;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.databind.SerializationFeature;
  import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  
  @Provider
  public class ObjectMapperContextResolver implements ContextResolver&lt;ObjectMapper&gt; {  
      private final ObjectMapper MAPPER;
  
      public ObjectMapperContextResolver() {
          MAPPER = new ObjectMapper();
          MAPPER.registerModule(new JavaTimeModule());
          MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
      }
  
      @Override
      public ObjectMapper getContext(Class&lt;?&gt; type) {
          return MAPPER;
      }  
  }
  </code></pre>
  
  <p>and got the following <strong>error</strong>:</p>
  
  <blockquote>
    <p>[org.springframework.web.servlet.PageNotFound] (default task-4) No
    mapping found for HTTP request with URI [/jbosswildfly/employee/list]
    in DispatcherServlet with name 'rest'</p>
  </blockquote>
  
  <p>Please can someone advise, I think my Spring configuration may not be correct. </p>
  
  <ul>
  <li>In the first case, with the <strong>@JsonFormat annotation</strong>, the request
  hits the RESTful service, but gets a <strong>NoSuchMethodError</strong> suggesting
  my dependencies are not correct.</li>
  <li>The second case, with the <strong>ContextResolver</strong>, the request ** doesn't
  find the DispatcherServelet** (and hence doesn't reach the RESTful
  service). This suggests my Spring config is incorrect.</li>
  </ul>
  
  <p>As I said, if I don't use the @JsonFormat annotation or the ContextResolver, I can call the RESTful service successfully (but I need to format the dates).</p>
  
  <p>Thank you</p>
  
  <p>My config as as follows:</p>
  
  <p><strong>pom.xml</strong></p>
  
  <pre><code>.
  .
  .
  &lt;jackson.version&gt;2.8.0&lt;/jackson.version&gt;
  .
  .
  .
          &lt;!-- JSON --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
              &lt;version&gt;${jackson.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
              &lt;version&gt;${jackson.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
              &lt;version&gt;${jackson.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-datatype-jsr310&lt;/artifactId&gt;
              &lt;version&gt;${jackson.version}&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;web-app version="3.1"
           xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
           metadata-complete="false"&gt;
  
           &lt;servlet&gt;
              &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
              &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
              &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
           &lt;/servlet&gt;
  
           &lt;servlet-mapping&gt;
              &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
           &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>rest-servlet.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
                             http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                             http://www.springframework.org/schema/context 
                             http://www.springframework.org/schema/context/spring-context-4.0.xsd
                             http://www.springframework.org/schema/mvc 
                             http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"&gt;
  
      &lt;context:component-scan base-package="com.jobs.spring" /&gt;
      &lt;mvc:annotation-driven /&gt;    
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Rest Controller</strong></p>
  
  <pre><code>@CrossOrigin(origins = {"*"})
  @RestController
  @RequestMapping(EmployeeRESTService.BASE_URI)
  public class EmployeeRESTService {
  
      public static final String BASE_URI = "/employee";
  
      @Autowired
      private EmployeeService employeeService;
  
      @RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
      public List&lt;Employee&gt; findAllEmployees() {
          return employeeService.findAll();
      }
  
      @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
      public @ResponseBody String saveEmployee(@RequestBody Employee employee){
          Long id = employeeService.save(employee);
          return Long.toString(id);
      }
  }
  </code></pre>