2

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.

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.

export class Item {

    public id:number;
    public name:string;
    public workDescription:string;
    public image:FormData;
    public comment:string;
}

I'm using the model-mapper library for deserialize the above object to following class in Java.

@Getter
public class ItemDto {

    private int id;

    private String name;

    private String workDescription;

    private CommonsMultipartFile image;

    private String comment;
}

This is my controller that receives the object that I send from the front-end:

@RequestMapping(value = "/items", method = RequestMethod.POST, consumes = { "application/json",
            "multipart/form-data" }, produces = "application/json")
    @ResponseBody
    @Transactional
    public ResponseEntity<String> createItem(@Valid @DTO(ItemDto.class) Item item,
            @RequestParam(value = "work", required = true) String workId,
            UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request) {
}

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).

However, when the request is sent from the front end, Jackson gives me the error:

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 org.springframework.web.multipart.commons.CommonsMultipartFile (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 org.springframework.web.multipart.commons.CommonsMultipartFile (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"])

My question is: What should I do to deserialize correctly the object sent in my backend?

On the other hand, if I use this controller, only to upload an image, it works without problems:

@RequestMapping(value = "/items/images", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    @Transactional
    public ResponseEntity<?> uploadItemImage(@RequestParam("item") String itemId,
            @RequestParam("file") MultipartFile multipartFile, UriComponentsBuilder uriComponentsBuilder) {
Some Implementation....
}

I have tried it in several ways and I can not get it to work. Thank you so much!

AlejoDev
  • 4,345
  • 9
  • 36
  • 67
  • 1
    Since `CommonsMultipartFile` has no default constructor for Jackson to convert it. I would add `@JsonIgnore` to the top of its declaration to avoid Jackson from trying to deserialize. Then it's up to you to then attempt to get the files in there. – Chol Nhial Aug 12 '18 at 22:24
  • check this https://stackoverflow.com/questions/21329426/spring-mvc-multipart-request-with-json – eHayik Nov 12 '19 at 17:25

0 Answers0