0

I am making a simple API using Spring. and i am getting this error while uploading and mapping file.

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

this is my Controller ->

@RequestMapping(value = "/Hi", method = RequestMethod.POST, produces = { "application/json" })
@ResponseBody
public BasicResponse UploadData(@RequestBody CropImageData cropImageData, HttpServletRequest request) {
    BasicResponse basicResponse = new BasicResponse();

    System.out.println(cropImageData.getCropId());

    return basicResponse;
}

My cropImageData model class ->

public class CropImageData {
    @JsonProperty("cropImages")
    private MultipartFile[] cropImages;

    @JsonProperty("cropId")
    private String cropId;

    public MultipartFile[] getCropImages() {
        return cropImages;
    }

    public void setCropImages(MultipartFile[] cropImages) {
        this.cropImages = cropImages;
    }

    public String getCropId() {
        return cropId;
    }

    public void setCropId(String cropId) {
        this.cropId = cropId;
    }   
}

this is how i am sending request via POSTMAN.

POSTMAN REQUEST

Avi Patel
  • 475
  • 6
  • 23

1 Answers1

0

With Postman, you are sending a HTTP post form-data but your end point is not configured to receive this format (the consumes = { "multipart/form-data" } annotation is missing).

Instead of your model class, you should change the signature of your method to something like that:

public BasicResponse UploadData(@RequestPart("cropId") String cropId, @RequestPart("cropImages")  MultipartFile file)
  • Yes i know that way i can get it, but i want to get data via passing POJO inside RequestBody. – Avi Patel Nov 27 '18 at 07:58
  • 1
    You cannot pass a File as JSON property but you could encode your image as Base64 and pass it in your JSON object (see https://stackoverflow.com/questions/34485420/how-do-you-put-an-image-file-in-a-json-object/34485762 ) – frenchoverflow Nov 27 '18 at 15:42
  • Depends of the size of your image but yes encoding/decoding your image will have a cost. I would recommend using form-data instead and build your object yourself instead of deserializing it directly. – frenchoverflow Nov 28 '18 at 03:06