2

I'm trying to create a web service using Maven. I used DarchetypeArtifactId=maven-archetype-quickstart and I have created the instances of the jetty on main class and it works

package com.rest.test;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class App {
    public static void main(String[] args) throws Exception {
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        Server jettyServer = new Server(8080);
        jettyServer.setHandler(context);

        ServletHolder jerseyServlet = context.addServlet(
             org.glassfish.jersey.servlet.ServletContainer.class, "/*");
        jerseyServlet.setInitOrder(0);

        // Tells the Jersey Servlet which REST service/class to load.
        jerseyServlet.setInitParameter(
           "jersey.config.server.provider.classnames",
           UploadFileService.class.getCanonicalName());

        try {
            jettyServer.start();`enter code here`
            jettyServer.join();
        } finally {
            jettyServer.destroy();
        }
    }
}

the problem is related with the code in UploadFileService class, what class package or dependencies should I used?

I tried with “jersey-multipart.jar” but I had a dependency conflict with Jetty.
I also tried with Servlet 3.0 API, but I couldn't get it work with embedded jetty.

I found several examples:

Trying to upload a file to a JAX-RS (jersey) server - great example but it is for jersey server

http://www.codejava.net/java-ee/servlet/java-file-upload-example-with-servlet-30-api - great example but it is for Tomcat server

http://examples.javacodegeeks.com/enterprise-java/rest/jersey/jersey-file-upload-example/ - great example but it is for web application

I really appreciate if anybody can give any help or any nitty-gritty.

Thanks in advance!

Note: it was marked as duplicate by peeskillet, IT IS NOT DUPLICATED! MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response is using tomcat and my question is about embedded jetty. The only coincidence is the class name UploadFileService.class


Hi all

I modified the class, now I have two class JettyServer where I set the server and UploadFile where i try to manage the POST

package com.rest.test.restprj;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ServerProperties;

public class JettyServer
{
public static void main(String[] args) throws Exception
{

    Server jettyServer = new Server(8080);

    // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.

    ResourceHandler resource_handler = new ResourceHandler();

    // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[]{ "./html/index.html" });
    resource_handler.setResourceBase(".");

    //Jersey ServletContextHandler


    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    ServletHolder jerseyServlet = servletContextHandler.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "com.rest.test.restprj");

    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, servletContextHandler, new DefaultHandler() });
    jettyServer.setHandler(handlers);

    try {
        jettyServer.start();
        jettyServer.join();
    } finally {
        jettyServer.destroy();
    }
}
}

Everything seems to be ok with the above class. But I got confused what i needed to register the MultiPartFeature. I got the error:

SEVERE: Following issues have been detected: WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.rest.test.restprj.UploadFile.postMsg(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.lang.Exception at index 0.

@Path("/fileUpload")
public class UploadFile {

    @POST
    //@Consumes("multipart/form-data")
    //@Consumes("image/jpeg")
    @Produces("text/plain")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response postMsg (
            @FormDataParam("file") InputStream stream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) throws Exception {


        String uploadedFileLocation = "./" + fileDetail.getFileName();


        // save it
        writeToFile(stream, uploadedFileLocation);

        String output = "File uploaded to : " + uploadedFileLocation;

        final Application application = new ResourceConfig()
                .packages("org.glassfish.jersey.examples.multipart")
                .register(MultiPartFeature.class);

        return Response.status(200).entity(output).type(MediaType.TEXT_PLAIN).build();

}

    private void writeToFile(InputStream uploadedInputStream,
            String uploadedFileLocation) {

            try {
                OutputStream out = new FileOutputStream(new File(
                        uploadedFileLocation));
                int read = 0;
                byte[] bytes = new byte[1024];

                out = new FileOutputStream(new File(uploadedFileLocation));
                while ((read = uploadedInputStream.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                out.flush();
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }

        }

} 
Community
  • 1
  • 1
vJos
  • 131
  • 1
  • 2
  • 12
  • Can you post the details of the "dependency conflict with jetty" statement? And also what you tried with the "servlet 3.0 api" version as well? (both of those should work) – Joakim Erdfelt Oct 28 '15 at 16:35
  • 1
    Note to use more than one value for the `jersey.config.server.provider.classnames`, use a single string separated by a comma. That's how you should register the `MultiPartFeature` mentioned in the duplicate – Paul Samsotha Oct 28 '15 at 19:33
  • The answer remains the same. You are using the wrong dependency, and when you do add the correct dependency, you need to register the multipart feature. Now you just lost the duplicate question where the answer actually was. You really think it makes a difference whether you are using Tomcat or Jetty? They are both servlet containers, and the problem in both questions is the same "How do I make multipart work with Jersey?". And just to be the bigger man, even though you decided to throw a tantrum for no reason, here is the duplicate again. – Paul Samsotha Oct 29 '15 at 01:49
  • http://stackoverflow.com/q/30653012/2587435 If you need help making it work, please describe your problem, and I will try to help. – Paul Samsotha Oct 29 '15 at 01:50
  • Thank you Joakim Erdfelt and peeskillet. I will try ones again following your advice , and let see if i cant make it work! – vJos Oct 29 '15 at 02:12
  • Hi all, I was trying to get it works, but i couldn't, I got the error: SEVERE: Following issues have been detected: WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.rest.test.restprj.UploadFile.postMsg(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.lang.Exception at index 0. – vJos Nov 04 '15 at 14:10

0 Answers0