title: Deserialization of MultipartFile from Angular to Java using Jackson
body: <p>I am developing a REST application using SpringBoot 1.5.9 and Angular 5, I need to send a REST request to SpringBoot back-end in order to save a object into the database.</p>
  
  <p>In the front-end, user fills up a form and submits. Following is the interface from which a object is created in order to be sent to the backend as a REST call.</p>
  
  <pre><code>export class Item {
  
      public id:number;
      public name:string;
      public workDescription:string;
      public image:FormData;
      public comment:string;
  }
  </code></pre>
  
  <p>I'm using the <strong>model-mapper</strong> library for deserialize the above object to following class in Java.</p>
  
  <pre><code>@Getter
  public class ItemDto {
  
      private int id;
  
      private String name;
  
      private String workDescription;
  
      private CommonsMultipartFile image;
  
      private String comment;
  }
  </code></pre>
  
  <p>This is my controller that receives the object that I send from the front-end:</p>
  
  <pre><code>@RequestMapping(value = "/items", method = RequestMethod.POST, consumes = { "application/json",
              "multipart/form-data" }, produces = "application/json")
      @ResponseBody
      @Transactional
      public ResponseEntity&lt;String&gt; createItem(@Valid @DTO(ItemDto.class) Item item,
              @RequestParam(value = "work", required = true) String workId,
              UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request) {
  }
  </code></pre>
  
  <p>As shown above, I need to accept the FormData sent from frontend as a CommonsMultipartFile (CommonsMultipartFile is an implementation of MultipartFile. That is why I used it).</p>
  
  <p>However, when the request is sent from the front end, Jackson gives me the error:</p>
  
  <blockquote>
    <p>2018-08-12 16:58:35.704  WARN 1833 --- [nio-8083-exec-1]
    .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP
    message:
    org.springframework.http.converter.HttpMessageNotReadableException:
    JSON parse error: Cannot construct instance of
    <code>org.springframework.web.multipart.commons.CommonsMultipartFile</code> (no
    Creators, like default construct, exist): cannot deserialize from
    Object value (no delegate- or property-based Creator); nested
    exception is
    com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
    construct instance of
    <code>org.springframework.web.multipart.commons.CommonsMultipartFile</code> (no
    Creators, like default construct, exist): cannot deserialize from
    Object value (no delegate- or property-based Creator)  at [Source:
    (PushbackInputStream); line: 1, column: 189] (through reference chain:
    com.espiritware.opusclick.dto.ItemDto["image"])</p>
  </blockquote>
  
  <p>My question is: What should I do to deserialize correctly the object sent in my backend?</p>
  
  <p>On the other hand, if I use this controller, only to upload an image, it works without problems:</p>
  
  <pre><code>@RequestMapping(value = "/items/images", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
      @Transactional
      public ResponseEntity&lt;?&gt; uploadItemImage(@RequestParam("item") String itemId,
              @RequestParam("file") MultipartFile multipartFile, UriComponentsBuilder uriComponentsBuilder) {
  Some Implementation....
  }
  </code></pre>
  
  <p>I have tried it in several ways and I can not get it to work. Thank you so much!</p>