I have been attempting (with no success thus far) to include an image file in my JSON data when making a call to a method in my webservice.
I have come across some threads discussing sending just an image, but not integrating an image within a JSON data object.
The call functions as expected if I keep "imageFile" as null.
$("#butSubmit").click(function(){
var files = document.getElementById('files').files;
var file = files[0];
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url:"http://localhost:8080/SampleApp/api/users/add",
data: JSON.stringify({"description":"test2","title":"testest2","uploaderName":"seren","imageFile":file})
});
});
On the web service side, I am utilizing this method:
@Path("/add")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void addUser(Picture picture){
userService.persistPicture(picture);
}
and this constructor for the Picture class:
@JsonCreator
public Picture(
@JsonProperty("description") String description,
@JsonProperty("title") String title,
@JsonProperty("uploaderName") String uploaderName,
@JsonProperty("imageFile") byte[] imageFile) {
this.uploaderName = uploaderName;
this.title = title;
this.description = description;
this.imageFile = (byte[]) imageFile;
}
Your guidance on this matter would be greatly appreciated!
Thank you in advance!