title: How to manually use springs default object mapper / manually deserializing of Http-Request Body to Object
body: <p>I have an generic Rest-Constroller which needs to construct an entity out of an entity-name an request body representing the entities properties.
  I need to manually map the request body to an target class.</p>
  
  <p>I already tried different ways to customize the way my RestController deserializes the data and think my current approach is the easiest.
  I now register an @PostMapping and retrieve the entityName which i use to determine the correct class and an @RequestBody String entityJson which i convert via an ObjectMapper to the determined class.</p>
  
  <p>yet i encounter problems during LocalDate deserialiing (Note: my date strings are of format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ' where times are zeros).</p>
  
  <p>I use springboot 2.1.6.RELEASE with jpa 2.1.9 and therefore do not need  custom handling of LocalDate/LocalDateTime properties in my RestControllers if i use an @RequestBody XXXEntity entity parameter.
  But when injecting an OnjectMapper bean to my RestController and try to objectMapper.readValue(entityJson, entityClass) deserialisation fails on LocalDate properties.</p>
  
  <p>How can i deserialize the way spring does (correctly) if the entity class is known upfront?</p>
  
  <p>1.) Exception without custom configuration:
  com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of <code>java.time.LocalDate</code> (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-10-29T00:00:00.000Z')</p>
  
  <p>2.) When using an custom JSR310Module ObjecktMapper</p>
  
  <p>java.time.format.DateTimeParseException: Text '2019-10-29T00:00:00.000Z' could not be parsed, unparsed text found at index 10</p>
  
  <pre class="lang-java prettyprint-override"><code>        ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
                  .serializationInclusion(JsonInclude.Include.NON_NULL)
                  .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) 
                  .modules(new JSR310Module())
                  .build();
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>Same error with custom DateFormat:
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
          objectMapper.setDateFormat(df);
  </code></pre>
  
  <p>I tried registering the LocalDateTime Deserializer for LocalDate das well:</p>
  
  <pre class="lang-java prettyprint-override"><code> Jackson2ObjectMapperBuilder.json()
      .deserializerByType(LocalDate.class, LocalDateTimeDeserializer.INSTANCE)
  </code></pre>
  
  <p>Result gets better... so paring fails where i hit the UTC timezon:
  java.time.format.DateTimeParseException: Text '2019-10-29T00:00:00.000Z' could not be parsed, unparsed text found at index 23</p>
  
  <p><strong>This is how my Controller looks like:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>    @Autowired
      public AdminRestController(EntityService entityService, ObjectMapper objectMapper) {
          this.entityService = entityService;
          this.objectMapper = objectMapper;
      }
  
  ...
  
      @Transactional
      @PostMapping(value = "/entities/{entityName}")
      public List&lt;T&gt; createEntity(@PathVariable String entityName, @RequestBody String entityJson) {
          T newEntity = constructEntity(entityName, entityJson);
          entityService.create(newEntity);
          ...
      }
  ...
      private T constructEntity(String entityName, String entityJson) {
          Class&lt;T&gt; entityClass;
          try {
              entityClass = findClassByName(entityName);
          } catch (ClassNotFoundException e) {
              throw new IllegalStateException("Entity " + entityName + " unknwon.");
          }
          try {
              T entity = objectMapper.readValue(entityJson, entityClass);
              return entity;
          } catch (IOException e) {
              throw new IllegalStateException("Unable to construct entity  " + entityName + " from provided data.", e);
          }
      }
  </code></pre>