I need to create a method in Spring MVC that can handle JSON and Multipart Form requests.
This is my method signature:
@RequestMapping(value = { "/upload_image" }, method = RequestMethod.POST) public @ResponseBody void uploadImage(final ImageDTO image)
The ImageDTO class is as follows:
public class ImageDTO { private String imageUrl; private Long imageId; private MultipartFile image; public String getImageUrl() { return imageUrl; } public void setImageUrl(final String url) { this.imageUrl = url; } public Long getImageId() { return imageId; } public void setImageId(final Long imageId) { this.imageId = imageId; } public MultipartFile getImage() { return image; } public void setImage(MultipartFile image) { this.image = image; } }
So, the scenario is that I need to support two scenarios: 1. Download the download from the form where the Content-Type is multi-part (all DTO members are non-zero) 2. Upload the image using JSON using ONLY imageUrl. In this case, the request body is as follows:
{ "imageId":"1236", "imageUrl":"http://some.image.url", "image":null }
The current implementation handles a multi-page request well, but when sending JSON, the ImageDTO object contains NULL in all its members.
Is it possible for the same method to process both types of content?
Thanks.
source share