Kind of new to rest web services. Particularly with file uploading. I get this error when trying to upload a file using a java restful service using Jersey 3.0.
01-Oct-2020 13:58:20.906 SEVERE [http-nio-8080-exec-54] org.apache.cxf.jaxrs.utils.JAXRSUtils.logMessageHandlerProblem No message body reader has been found for class org.glassfish.jersey.media.multipart.FormDataContentDisposition, ContentType: multipart/form-data;boundary=---------------------------3611873841451717332877493500 01-Oct-2020 13:58:20.906 WARNING [http-nio-8080-exec-54] org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper.toResponse javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type
I know this question has been asked numerous times but none of the solutions seem to work. So I will be posting my code, etc. Any help appreciated. Let me know if you need more info. Thanks in advance!
I am deploying this service on Apache Tomcat (TomEE)/9.0.37 (8.0.4) I am using Eclipse Neon.3 Release (4.6.3). I am not using maven on this project currently.
here is my service class
@Path("/")
public class FileUpload extends Application{
/** The path to the folder where we want to store the uploaded files */
private static final String UPLOAD_FOLDER = "c:/projects/";
public FileUpload() {
}
@Context
private UriInfo context;
/**
* Returns text response to caller containing uploaded file location
*
* @return error response in case of missing parameters an internal
* exception or success response if file has been stored
* successfully
*/
@Path("/upload")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile
(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) throws Exception{
// check if all form parameters are provided
System.out.println("Inside uploadfile");
if (uploadedInputStream == null || fileDetail == null)
return Response.status(400).entity("Invalid form data").build();
// create our destination folder, if it not exists
try {
createFolderIfNotExists(UPLOAD_FOLDER);
} catch (SecurityException se) {
return Response.status(500)
.entity("Can not create destination folder on server")
.build();
}
String uploadedFileLocation = UPLOAD_FOLDER + fileDetail.getFileName();
try {
saveToFile(uploadedInputStream, uploadedFileLocation);
} catch (IOException e) {
return Response.status(500).entity("Can not save file").build();
}
return Response.status(200)
.entity("File saved to " + uploadedFileLocation).build();
}
/**
* Utility method to save InputStream data to target location/file
*
* @param inStream
* - InputStream to be saved
* @param target
* - full path to destination file
*/
private void saveToFile(InputStream inStream, String target)
throws IOException {
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(target));
while ((read = inStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
}
/**
* Creates a folder to desired location if it not already exists
*
* @param dirName
* - full path to the folder
* @throws SecurityException
* - in case you don't have permission to create the folder
*/
private void createFolderIfNotExists(String dirName)
throws SecurityException {
File theDir = new File(dirName);
if (!theDir.exists()) {
theDir.mkdir();
}
}
}
Below is my Application class. Not even sure if I need this or not. Someone suggested I did.
package edu.pcom.docusign;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("")
public class DemoApplication extends ResourceConfig{
DemoApplication(){
register( MultiPartFeature.class );
registerClasses( FileUpload.class );
}
}
There is no web.xml because I am using annotations.
These are the libs I am using.
aopalliance-repackaged-3.0.0-M2.jar
hk2-metadata-generator-3.0.0-M2.jar jaxrs-ri-3.0.0-M6.jar
jersey-media-jaxb-3.0.0-M6.jar class-model-3.0.0-M2.jar
hk2-utils-3.0.0-M2.jar
jersey-bean-validation-3.0.0-M6.jar
jersey-media-moxy-3.0.0-M6.jar guice-4.2.3.jar
hk2-xml-3.0.0-M2.jar
jersey-media-multipart-3.0.0-M6.jar hk2-3.0.0-M2.jar
javassist-3.27.0-GA.jar
jersey-client-3.0.0-M6.jar
jersey-server-3.0.0-M6.jar hk2-api-3.0.0-M2.jar
javax.annotation-api-1.3.2.jar
jersey-common-3.0.0-M6.jar mimepull-1.9.13.jar hk2-core-3.0.0-M2.jar
javax.inject-2.5.0-b62.jar
jersey-container-servlet-3.0.0-M6.jar
osgi-resource-locator-2.5.0-b42.jar hk2-extras-3.0.0-M2.jar
javax.servlet-api-4.0.1.jar
jersey-container-servlet-core-3.0.0-M6.jar validation-api-2.0.1.Final.jar hk2-locator-3.0.0-M2.jar
javax.ws.rs-api-2.1.1.jar jersey-hk2-3.0.0-M6.jar