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();
}
}
}