5

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??

Goldbones
  • 1,407
  • 3
  • 21
  • 55
  • how are you trying to invoke the endpoint ? – Alejandro Agapito Bautista Jul 16 '15 at 21:34
  • What do you mean? I try to run my app and this error appears before I can do anything – Goldbones Jul 16 '15 at 22:25
  • 2
    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). I don't work much with Ant, so I'm not sure how you can do that. – Paul Samsotha Jul 17 '15 at 08:34
  • You can also ignore the model validation errors with the init-param `jersey.config.server.resource.validation.ignoreErrors` set to true. I don't recommend this for production, but at least the app might start. Try to hit the endpoint and see what happens. See if you get any exceptions. – Paul Samsotha Jul 17 '15 at 08:43
  • Perfect. Worked as expected! Please add an answer instead comment. You deserve your points! – Goldbones Jul 17 '15 at 11:01

2 Answers2

2

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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • hello @peeskillet , i get this error : javax.ws.rs.NotSupportedException: Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form-data; when i'm trying to upload my file to rest service, i'm using the same code that you used (i'm using wildfly) , can you help me please? thanks in advance. – James Jun 19 '16 at 18:05
0

Below code worked for me:

Class ->>> add it

Class Property --->> add it

Public Class userREST () {

@POST
    @Path("upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response uploadImageFile(@FormDataParam("uploadFile") InputStream fileInputStream,
            @FormDataParam("uploadFile") FormDataContentDisposition fileFormDataContentDisposition,
            @FormDataParam("FIR_REG_NUM") String FIR_REG_NUM, @FormDataParam("LOGIN_ID") String LOGIN_ID) {

        final_json_result = WriteFileInFolder.fileAnalysis(fileInputStream, fileFormDataContentDisposition, FIR_REG_NUM,
                LOGIN_ID);

        return Response.ok(final_json_result).build();

    }// uploadImageFile

package ####.jaxrs.jwt;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import ####.helper.Common@@@;
import ####.jaxrs.jwt.filters.JWTRequestFilter;
import ####.jaxrs.jwt.filters.JWTResponseFilter;
import ####.service.FileServicesREST;



@ApplicationPath("fileservice")
public class FileJAXRSConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {

        Common@@@.logging("@ApplicationPath@FileServicesREST...");
        Set<Class<?>> clazzes = new HashSet<Class<?>>();
        clazzes.add(JWTRequestFilter.class);
        clazzes.add(FileServicesREST.class);
        clazzes.add(JWTResponseFilter.class);

        return clazzes;
    }


    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("jersey.config.server.provider.packages", "####.service");
        properties.put("jersey.config.server.provider.classnames", "org.glassfish.jersey.media.multipart.MultiPartFeature");
        return properties;
    }

}

Don't need to add following in web.xml

<init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>mha.@@@.service</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
        </init-param>
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51