I want to return response in XML form from REST API in java -
i have rest api in java take image file , save on server want return path of uploaded image in xml form don't know how it.currently returns response string in browser.
here code.
package com.javacodegeeks.enterprise.rest.jersey; import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import javax.ws.rs.consumes; import javax.ws.rs.post; import javax.ws.rs.path; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.response; import com.sun.jersey.core.header.formdatacontentdisposition; import com.sun.jersey.multipart.formdataparam; @path("/files") public class jerseyfileupload { private static final string server_upload_location_folder = "/home/hassan/downloads/"; /** * upload file */ @post @path("/upload") @consumes(mediatype.multipart_form_data) public response uploadfile( @formdataparam("file") inputstream fileinputstream, @formdataparam("file") formdatacontentdisposition contentdispositionheader) { string filepath = server_upload_location_folder + contentdispositionheader.getfilename(); // save file server savefile(fileinputstream, filepath); string output = "file saved server location : " + filepath; return response.status(200).entity(output).build(); } // save uploaded file defined location on server private void savefile(inputstream uploadedinputstream, string serverlocation) { try { outputstream outpustream = new fileoutputstream(new file(serverlocation)); int read = 0; byte[] bytes = new byte[1024]; outpustream = new fileoutputstream(new file(serverlocation)); while ((read = uploadedinputstream.read(bytes)) != -1) { outpustream.write(bytes, 0, read); } outpustream.flush(); outpustream.close(); } catch (ioexception e) { e.printstacktrace(); } } }
if want return xml rest, try create object fields. , object , field have @xmlrootelement @xmlelement , put @produces("application/xml") on top of method signature.
@post @path("/upload") @consumes(mediatype.multipart_form_data) @produces("application/xml") public response uploadfile(...){ //body }
also can use @produces(mediatype.application_xml)
instead of @produces("application/xml")
. both same.
Comments
Post a Comment