1

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

Jsowa
  • 9,104
  • 5
  • 56
  • 60
  • 1. Why does the FileUpload class extend Application? 2. Why is the DemoApplication constructor not public? – Paul Samsotha Oct 01 '20 at 17:41
  • [This](https://stackoverflow.com/q/45625925/2587435) might help you out. – Paul Samsotha Oct 01 '20 at 17:44
  • Hmmm.....good question about 1. I will try that. – user2170680 Oct 01 '20 at 17:45
  • made the contructor public and it still not work. This is the entire error .01-Oct-2020 13:47:17.929 SEVERE [http-nio-8080-exec39]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=---------------------------146402206312988606612285458025 01-Oct-2020 13:47:17.930 WARNING [http-nio-8080-exec-39] org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper.toResponse javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type – user2170680 Oct 01 '20 at 17:48
  • You are not using Jersey. You are using CXF (You can tell by the cxf in the package name in the exception). CXF if the default JAX-RS implementation that comes with TomEE. If you use vanilla Tomcat, Jersey will work with your current configuration. Otherwise, you will need to use a web.xml to make Jersey work. See my previous link. Use one of the options with a web.xml – Paul Samsotha Oct 01 '20 at 17:56
  • interesting because I tried previous versions of tomcat from v8 to 10 and it did not work. I will try the use of a web xml file again and try. thank you for looking at this – user2170680 Oct 01 '20 at 18:12
  • so from what I am trying to do.What would you recommend as far as jar files are concerned? Can you list all I should use if I am trying to implement this on tomcat 8 or 9? – user2170680 Oct 02 '20 at 14:07
  • You have all the jars (all the ones from [the bundle here](https://eclipse-ee4j.github.io/jersey/download.html)). You have the code needed. Just make your constructor public like I mentioned and remove the Application extension and you're good to go. Make sure your jars are added to the war. – Paul Samsotha Oct 02 '20 at 14:24
  • @user2170680 how did you fix the issue? i am facing this issue now – din_oops May 29 '22 at 14:49

0 Answers0