Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Upload a file using Jax-RS

I want to use jersey framework. I´m running a web Service, using an ant app, on Java EE7. My application server is Glassfish

My method look like this:

 package mypackage.service;
        ...
         import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
        import org.glassfish.jersey.media.multipart.FormDataParam;

        @POST
        @Path("createSomething")
        @Consumes(MULTIPART_FORM_DATA)
        @Produces(APPLICATION_XML)
        public Response createSomething(@FormDataParam("upload") InputStream is, @FormDataParam("upload") FormDataContentDisposition formData, @QueryParam("some") String some,  @Context HttpServletRequest request) {

            String fileLocation = "C:\\UploadFile\\" + formData.getFileName();

        //more things, do not matter

            try {
                ctrl.saveFile(is, fileLocation);
                String result = "Successfully File Uploaded on the path " + fileLocation;
                return Response.status(Response.Status.OK).entity(result).build();
            } catch (IOException e) {
                e.printStackTrace();
                return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

            }

I also have an application config:

package mypackage.service;

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

@javax.ws.rs.ApplicationPath("")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> resources = new HashSet<>();
        addRestResourceClasses(resources);
        resources.add(MultiPartFeature.class);

        return resources;

    }

    /**
     * Do not modify addRestResourceClasses() method. It is automatically
     * populated with all resources defined in the project. If required, comment
     * out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(mypackage.service.MYSERVICE.class);


    }

}

On myweb.xml I have:

<servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>mypackage.service.ApplicationConfig</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>mypackage.service</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/createSomething/*</url-pattern>
</servlet-mapping>

I still get the same message:

Caused by: org.apache.catalina.LifecycleException: org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response

What I´m doing wrong??

like image 777
Goldbones Avatar asked Oct 30 '22 23:10

Goldbones


1 Answers

It works fine for me. Though I would completely get rid of the Application subclass. It is not needed, and may cause conflict/confusion. Your xml is sufficient configuration, just get rid of the javax.ws.rs.Application <init-param>. I would also look into making the multipart jars only compile-time jars (meaning not built into the war - they might conflict with Glassfish's version). I don't work much with Ant, so I'm not sure how you can do that, but I know it's possible.

like image 176
Paul Samsotha Avatar answered Nov 13 '22 00:11

Paul Samsotha